SyntaxHighlighter JS

2013-01-19

Notes on Nested Classes

Use if class is only used with/by parent class.
Four kinds of nested classes, in order of preference.
  1. 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()
  2. 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() 
  3. 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
  4. 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