Inner Classes

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

(not Completed)

Topic Notes
Inner Classes
  • New feature of JDK 1.1
  • inner class - classes that can be defined in any scope.
  • Scopes:  defined as a member of another class, or within a block of statements (a method) or anonymously (no name) within an expression (statement).
  • Inner classes have access to members within the enclosing scope.
  • Members in inner classes cannot be static.  In otherwords, only top level classes may have statics.
  • A static method in a top level class can access an inner class if that class is defined as static.
    Ex:  static class MyIo_WindowHandler extends WindowAdapter
Types of classes Types of classes:
  • Top Level - normal class.  A class that is not within another class.  A class defined at the package level.
    Example of .class naming:
    TopLevelName.class
  • Outer Class - a class that has another class inside it. A top level class is an outer class.  Another example is inner class that has an inner class inside it.
  • Inner Class, at class level - a class defined within another class, but not within a method.
    Example of .class naming:
    TopLevelName$InnerName.class
  • Inner Class, at method level - a class defined within another class, but not within a method.
    Example of .class naming: (same as at class level)
  • Anonymous Class - a class defined within another class, except it has no name.
    Example of .class naming:
    TopLevelName$1.class - name of first anonymous .class
    TopLevelName$2.class -      "        second       "   (etc...)
Inner Class, at class level
  • Can access all of the instance variables of the top level class

Example:

  • class MyTopClass {
       // [Pub: Top Class Body here !!! ]
       MyTopClassMethod() { }  //Example of a method!

       class MyInnerClass {
           // [ Put:  Inner Class Body here !!! ]
       }   //end of MyInnderClass
    } //end of MyTopClass
Inner Class, at method level
  • Can access all of the instance variables of the top level class along with any local variables in the method.
  • ??? (Marcus Green) Can only access variables at the level of the enclosing method if they have been defined as final. This includes variables passed as parameters to the enclosing method.

Example:

  • public void MyMethod() {
    class MyInnerClassMethod {}
    }
Anonymous Class
  • Can access all of the instance variables of the top level class along with any local variables in the method it is in.
  • Anonymous classes cannot have constructors.
  • Great for Event handeling.

Example of Event handeling:

  • class MyTopClass {
       MyMethod()
       { new ClassType() { //Class Body}    //Inner Class
       }
    } //end of MyTopClass
  • //Anonymous Inner class to handle the event.
    butFirst.addActionListener( new ActionListener()
    { public void actionPerformed(ActionEvent e)
    { cl.first( panCards );
    System.out.println("Pressed butFirst");
    }
    } ); //inner class for addActionListener