Java - Things to know before your OCJP exam - Inner Classes

1. Static inner class cannot access instance members of the enclosing class.

2. Local class declared within static method cannot access instance members.

3. * Static inner class can be accessed without instance of the parent class.

4. * In case of local inner classes , we can even instantiate using new with the interface.

new runnable (){

public void run() {}


is valid whereas

new runnable(); isn't

5. An inner class can access all elements of the parent class , even declared private.

6. * To instantiate the inner class object following code is used

OuterClass.InnerClass innerRef = InnerClass();

7. From code within the inner class, this refers to inner class. To access outer class we should do OuterClass.this.

8. An anonymous inner class can either extend one class or implement one interface only.

9 Anonymous inner class should be ended with a semi colon after curly brace.

10. * Static inner class can be instantited like this

OuterClass.InnerClass inner = new OuterClass.InnerClass();