Four kinds of nested classes, in order of preference.
- static member classes
* Access to all enclosing class static members, even private.
Can acces other static peer member class fields.
* Same visibility rule as regular static members.
* Can be an interface
* If member class does not require access to enclosing instance, always make member class static. Save time and space on enclosing class construction.
* Example, Map.Entry
* Usage syntax: new Classname.StaticMemberClass() - nonstatic member classes
* Access to all enclosing class static and instance members, even private.
* Same visibility rule as regular instance members.
* Cannot be interface
* No static fields or methods, except for static final.
* Can obtain explicit access to enclosing class instance members via
Classname.this.instanceMember
* Can obtain explicit access to the super class of the enclosing class via
Classname.super.instanceMember
* Usage syntax: classname.new NonStaticMemberClass() - anonymous classes
* Normally used as function objects
* Good if class is short, used once, and right away.
* No constructor but can use instance initializer
* No static fields or methods, except for static final - local classes
* Classes defined inside methods.
* Can only use final fields of enclosing method.
* Access to all enclosing class static and instance members, even private.
* No static fields or methods, except for static final.
No comments:
Post a Comment