As the name itself indicates that inner class with non-static scope that means a member of outer class which is of instance scope called as instance inner class. Instance inner class is itself a class but we cannot use any static member.

Why we cannot use any static member in instance inner class ?
--> It is first treated as the member of outer class. Because of this if it is declared as non-static that means JVM is not going to load the class while loading the outer class even if inner class is a class. So as the non-static member of outer class, at the time of class loading JVM will not be able to load the instance member and hence if JVM will not load it then no static member can be able to initialized at the class loading time and hence we cannot use any static member inside an instance inner class.

But can be able to define an static final variable as because it will be treated as constant. And there is no memory concern of final static variable hence we can use it uses directly the value instead through any variable.

We can define an instance member as it will be used when we are creating any object for instance inner class and hence there is no risk of declaring as instance memory for instance variable is not created at the time of class loading.

Important points regarding inner class.

1. To access inner class member we need to create an object of it as we cannot access it through outer class object.

2. Member of outer class cannot be accessed with the inner class object.

3. outer class member can be accessed directly with its name as internally this will be used to indicate current class object.

4. We cannot use any static member in the instance inner class.

5. Instance inner class cannot use be the starting point of any java program as we cannot use main() which is declared as static.

6. But we can be able to use static final member as it is always treated as constant an also it does not require any memory for its value storage.

What will happen if we try to use main() in instance inner class?
--> While compiling the java program it will gibe an compilation error as " inner class cannot have any static declarations ".

Is it possible to use instance inner class as the starting point of our program ?
--> No , as because we can't be able to use any static method inside instance inner class.

An example to show how to access to create an object and how to access the member of it .

public class A{

   int x;

class B{                           // instance inner class

    int x;                        // instance member of instance inner class.

   static final int z= 30;        // final static member

}

A(){                              // outer class constructor

   A.B a = new A.B();            // inner class object creation .

   a.x=30;                       // inner class instance member 
                                     initialization

   System.out.println(a.x);

   System.out.println(this.x);   // this refer to the outer 
                                    class instance member.

   System.out.println(a.z);     // accessing the static final 
                                   instance member.

}

public static void main(String[] args) {

 A p = new A();

     }
}

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic