| |
[2.4.5] Inner(non static nested) class (not top level, I) | A nested class is declared inside curly braces of another class. An inner class is a nested class that is not explicitly or implicitly (eg member of an interface ) declared static. |
[2.4.1] Method-local inner (L) | Defined within a method of the enclosing class |
[2.4.2] Order for instantiate and use (L) | Complete class definition, then instantiate and use!! |
[2.4.3] Access to which variables (L) | final. Even if final variable x is present in outer class, if defined within a static method, the method cannot access x without this |
[2.4.4] Only modifiers (L) | either final or abstract |
[2.4.0] How to remember (Rules for inner classes) | L-I-S-A |
[2.4.6] Only modifiers (I) | All modifiers like other members + final or abstract |
[2.4.7] Relationship with O (I) | Inner class can access even the private members of O |
[2.4.8] Order for instantiate and use (I) | MyInner mi = new MyInner // from within class
MyOuter mo= new MyOuter(); MyOuter.MyInner inner = mo.new Inner(); // from outside class
use reference to Outer ('this' if referring to Inner, Outer.this if referencing Outer instance) |
[2.4.9] Anonymous Inner (A) | subclass of named type OR implementer of named interface }); |
[2.4.10] use (A) | only those defined in reference variable type |
[2.4.11] restricted how? (A) | either extend one class or implement exactly one interface. (anonymous class is implicitly final) |
[2.4.12] instantiation (A) | declared, defined and instantiated as part of a method invocation- ended by } and ) for method call and ; for statement |
[2.4.13] static nested class (is top level, S) (SIC alert - do not say static inner class!!!!) | inner class but with static modifier, really not inner class! A static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. |
[2.4.14] Relationship with O (S) | None (Declaring a nested class static only means that instances of the class are created without having an outer instance. ) |
[2.4.15] instantiation (S) | BigOuter.Nested x = new BigOuter().Nested(); |
[2.4.16] restricted how? (S) | cannot access nonstatic members of O because no implicit reference ie outer this (instances of the class are created without having an outer instance. The members of the static class can be static or not.) |
[2.4.17] Only modifiers (S) | Static nested classes can be declared as final or abstract |
[2.4.18] Polymorphism (S) | Can extend; can be extended |
[2.4.19] Members (S) | Only type of nested class that can have static members |
[2.4.20} Nested classes | Cannot access nested class elements without an instance of outer class (eg Outer.this.x) |
[2.4.21] Interface | Can only have static nested class since interface members are implicitly public and static |
[2.4.22] Remember that if class implements interface it implicitly extends Object and hence cannot extend other ref 2.4.11 | If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class. |
[2.4.23] compile time constants | Earlier, Non-static inner classes were not allowed to have static fields at all. This rule is now modified and as per the new rule inner classes are allowed to declare static final fields that are compile time constants as members. |