Java | Static and Non-Static Inner classes

Author - Komal
21.03.2017
|
0 Comments
|

Static and Non-Static Inner classes:

Static Nested Classes:

Static Nested Classes are the static classes in Java which are created inside another outer class. As these are declared inside another class with keyword ‘static’, therefore they are treated like the static member of its outer class.

For Nested Classes, OuterClassName$NestedClassName.class file automatically gets created. JVM treats it just like a class name. JVM does not know about which class is the nested class of which class. For example, if the outer class name is Outer and nested class name is Nested then Outer$Nested.class file gets created automatically.

‘Static’ keyword in static Nested Classes does not mean that it will be loaded in class area. JVM treats it just like another class. As it is only a blueprint, it does not take any memory in class area.

Nested Classes can also access the private members of the Outer class:

For Example:

class Outer {
   int a = 10;
   static int b = 20;
   static class Nested {
       int c = 30;
       static int d = 40;
       public void show() {
           System.out.println(“A: ”+a); //Not Possible : Error
           System.out.println(“B: ”+b); //Possible
           System.out.println(“C: ”+c);
           System.out.println(“D: ”+d);
        }
    }
}
class Main {
    public static void main(String[] args) {
    Outer.Nested obj = new Outer.Nested(); //creating object of Static Nested Class
    obj.show();
    }
}

In above example, Nested is the static nested class which is declared inside Outer class.

To access the static Nested Class, we need to create the object of that class through:

Outer.Nested obj=new Outer.Nested();

Here, Outer.Nested is used because Nested is the static class inside Outer class. As static variables can’t be accessed with the object of outer class, therefore, we need to access it using dot operator along with Outer class name.
Here, a is not directly accessible in nested class because it is an instance variable of Outer class so cannot be used without the object of Outer class.

It can be possible through:

static class Nested {
    Outer outer_object = new Outer();
    -- -- --
    public void show() {
    System.out.println(“A: ”+outer_object.a); //accessing instance variable of Outer
    }
}

Variable b is accessible directly as it is a static variable and is not bound to the object of Outer class.

Non-Static Nested Classes:

Non-static nested classes are bound to the object of the outer classes. These are also called as Inner Classes. These classes do not allow static variables and methods in it as it is bound to the object of the outer class and exist only if the object of the outer class is created.
The use of inner classes makes the code more readable and maintainable as it is used to logically group classes in one place.

Non-static nested classes have full access to the class members in which it is created. A single instance of the outer class can be associated with more than one instances of the inner class.
The keyword ”this” can be used in the non-static method to refer to the current object. To access the outer class instance inside the method show(), we write Outer.this.

For Example:

class Outer {
    int a = 10;
    static int b = 20;
    class Nested {
        int c = 30;
        static int d = 40; //Not Possible : Error
        public void show() {
            System.out.println(“A: ”+a);
            System.out.println(“B: ”+b);
            System.out.println(“C: ”+c);
            System.out.println(“D: ”+d); //Not allowed
        }
    }
}

class Main {
    public static void main(String[] args) {
        Outer.Nested obj = new Outer().new Nested(); //creating object of Inner Class
        obj.show();
    }
}

In this example, Nested is the non static nested class (Inner Class). It is accessible only by creating the object of the Outer class. Here in the line:

Outer.Nested obj=new Outer().new Nested();

Firstly, object of Outer class is created then through that object, the object of inner class Nested get created.

Here, method show() of non-static inner class can directly access the member variables a,b and c of both outer and its own class.

These two types of classes also are the non-static nested classes:

Method-local Inner Classes:

Method-local Inner Classes are the classes that are created inside the method of outer class :

For Example:

Class Outer {
    int a;
    public void method_name() {
        Class Method_InnerClass {
            void innerMethod() {
                //Method Code
            }
        }
    }
}

Here in this short example, Method_InnerClass is the inner class that is created inside the method of outer class.

Anonymous Classes:

Anonymous classes are the classes that are used to override the functionality of method of a class or interface. These classes do not have any classname that is why they are called as anonymous classes.
These classes are declared and instantiated together in a single line. These classes can be instantiated only once.

For Example:

interface Anonymous_ClassName {
    public void anonymous_method();
}

public class AnonymousOuterClass {
    public static void main(String[] args) {
        Anonymous_ClassName anonymous_object = new Anonymous_ClassName() {
            public void anonymous_method() {
                System.out.println("Anonymous Class Example");
            }
        };
        anonymous_object.anonymous_method();
    }
}

Here, Anonymous_ClassName is the interface having method anonymous_method() which is then overridden in the anonymous class declared and instantiated inside main method in the class AnonymousOuterClass.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *