Sunday, June 15, 2014

Java Inner classes - local, inner, anonymous

There are three types of inner classes.
  • Member Classes
  • Local Classes
  • Anonymous Classes

Member Classes (Non-static Inner Classes)


  1. A member class can access all the fields and methods (even private) of an enclosing class. 
  2. A member class can be declared as public, protected, private, abstract, final, or static. 
  3. When you declare a member class with a static modifier it will not be an inner class anymore. It will be a top-level nested class.


They are declared within a normal class

class Outer { // Top-level class
   class Inner {// without static modifier 
                // Member or Non-static inner
                // class
   }
}

They must have a INSTANCE of an outer class

Outer.Inner theInner = new Outer().new Inner();//or
Outer
theOuter = new Outer();
Outer.Inner theInner = theOuter.new Inner();


Inner class METHODS cannot be static. 

//First code snippet with a static member.
class Outer1 { // Top-level class
   class Inner1 {// without static modifier 
                // Member or Non-static inner
                // class
                static int x; // ERROR!
   }
}


this is ok

class Outer2 { // Top-level class
   class Inner2 {// without static modifier 
                // Member or Non-static inner
                // class
               int x; // OK!
   }
}

this is also ok

class Outer3 { // Top-level class
   class Inner3 {// without static modifier 
                // Member or Non-static inner class.
                // compile-time constant
               static final int x = 0; // OK!
   }
}

LOCAL Classes  (like a local variable, when accessing local variables they must be final)

Declared in Method, constructor, static initializer or a instance initializer

Can access everything in the enclosing class but only the final vars of the method

enclosing class cannot see it...

can be in a static method but not declared static

e.g.

class Outer { // Top-level class
   void method() { // Method declaration
      class Inner {// Local class 
      }
     }
}

Anonymous Class (why do we have this sturff)

has no name duh anonymous - 
since it has no name there is no contructor - try it... um err

Anonymous classes do not allow the use of extends and implements clauses and access modifiers
but...
they can either extend or implement an interface not both
general form is:
//interface implementation
 new InterfaceName() {}//extends a class
 new ClassName() {} 

The interface name is the name of the interface that is going to be implemented, and the class name is the name of the class that is going to be extended. They are not the name of our newly created class. It is the name of the class that we intend to implement or extend.


Exampool!  example noob

//...
   void initUserInterface() { // Method                                     //declaration
      //... 
      addWindowListener(//Method begins 
                  //class delaration 
                  new WindowAdapter(){  
                         //implemetion for the method 
                      public windowClosing(WindowEvent e)             {  
                              System.exit(0);  
                         }  
                  }  

            );//Method ends 
      //... 
      }
     }
//...








No comments:

Post a Comment