Home : Course Map : Chapter 7 : Java :
Inner Classes
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

A new technique that came with Java 1.1 was the nesting of classes. That is, a so-called inner class can be defined and instantiated all inside another classes.

Inner classes fall into four rough categories: static and instance member classes, local classes, and annoymous classes.

Member classes are included in the class definition just like fields and methods. A member class can be static with access only to the static members of the class to which it belongs, or it can be an instance class with access to both the static and instance members of the class that contains it.

An local class is defined within a code block, typically in a method. Like the value of local variable, an instance of a local class exists only during the processing of the method unless a reference to it is maintained. For example, in the following code we show how to put the definition of AnInnerClass inside a constructor:

public class AnOuterClass
{
  int fA;
  public AnOuterClass ()
  {
    ... other code ...

    class AnInnerClass
   
{
      int b;
      
AnInnerClass () {
        b = 4. * fA;
      }
    }
// class AnInnerClass

    AnInnerClass inner = new AnInnerClass ();
    ...
  } // ctor

  void outerMethod () {
    ...
  } // outerMethod
} // class AnOuterClass

The scope of the inner classes includes the instance variables and methods of the parent class.

A common use of inner classes is in event handling. The new event model allows us to make any class into a listener for our events. However, this can lead to listener classes spread out through the code and making the programs less readable.

With inner classes, we can put the listener class definition adjacent to the code for the component that uses the listener. For example, in the following code segement, an ActionListener class is placed next to where an instance of it is added to a button::

public class AnOuterClass extends java.applet.Applet
{
  Button fBt = new Button ("OK");
  public AnOuterClass ()
  {

    int i=0;
    class AnInnerClass implements ActionListener
    {
      public void
actionPerformed (ActionEvent e) {
        i++;
        System.out.println ("Pressed "+i+" times");
      }
    }

    bt.addActionListener (new AnInnerClass ());
    add (bt);
  } // class AnInnerClass

} // class
AnOuterClass

The compiler will separate out these inner classes and create separate class files for them. The names will be prepended with the outer class name. After you compile the above code, you would find the following file in the directory:

  AnOuterClass$AnInnerClass.class

 

Latest update: Nov. 3, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.