Static Inner Class in Java


The static keyword is used on a class, method, or field to make them work independently of any instance of the class.Static fields are common to all instances of a class. They do not need an instance to access them.

Static methods can be run without an instance of the class they are in. However, they can only access static fields of that class. Static classes can be declared inside of other classes. They do not need an instance of the class they are in to be instantiated.

Source Code

//Static Inner Class
class OuterClass {
    static int x=10;
    int y=20;
    static class InnerClass
    {
        void display()
        {
            System.out.println("X : "+x);
        }
    }
}
 
public class staticInnerClass {
    public static void main(String[] args) {
        OuterClass.InnerClass i =new OuterClass.InnerClass();
        i.display();
    }
}
 

Output

X : 10
To download raw file Click Here

Basic Programs