HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Using Inner Classes"
Vol. 2, Issue 4, p. 8

	

Listing 1: An example without using inner classes

   import java.awt.*;
   import java.awt.event.*;
   //Top-most class
   class ImageViewer {
      public static final void main(String[] args){
         ImageViewer im = new ImageViewer();
      }
      public ImageViewer(){
         CommandPanel cp = new CommandPanel(this);
      }
      public void displayImage(){
         // Code to display an image
      }
   }
   // Creates GUI
   class CommandPanel extends Frame{
      ImageViewer viewer;
      CommandPanel(ImageViewer vw){
         viewer = vw;
         createGUI();
      }
      private void createGUI(){
         setLayout(new FlowLayout());
         Button dispBtn = new Button("Display");
         DisplayCommand dispC = new DisplayCommand(viewer);
         dispBtn.addActionListener(dispC);
         // Other components
         add(dispBtn);
         pack();
         setSize(100,100);
         setVisible(true);
      }
   }
   //Adapter class to handle events
   class DisplayCommand implements ActionListener {
      ImageViewer viewer;
      public DisplayCommand(ImageViewer vw){
         viewer = vw;
      }
      public void actionPerformed(ActionEvent e){
         viewer.displayImage();
      }
   }

Listing 2: The example in listing 1 rewritten using inner classes

   import java.awt.*;
   import java.awt.event.*;
   //Top-most class
   class ImageViewer {
      public static final void main(String[] args){
         ImageViewer im = new ImageViewer();
      }
      public ImageViewer(){
         CommandPanel cp = new CommandPanel();
      }
      public void displayImage(){
         // Code to display an image
      }
      class CommandPanel extends Frame{
         CommandPanel(){
            createGUI();
         }
         private void createGUI(){
            setLayout(new FlowLayout());
            Button dispBtn = new Button("Display");
            DisplayCommand dispC = new DisplayCommand();
            dispBtn.addActionListener(dispC);
            // Other components
            add(dispBtn);
            pack();
            setSize(100,100);
            setVisible(true);
         }
         class DisplayCommand implements ActionListener {
            public void actionPerformed(ActionEvent e){
               displayImage();
            }
         } // End of DisplayCommand
      } // End of CommandPanel
    } // End of ImageViewer


Listing 3: Top level and inner class examples

// A top-level class
class OuterMost{
   private String str = new  String("OuterMost");
   public void printName(){
      System.out.println("Class name: "+ str);
   }
   //A static class
   static class StaticNestedOne{
      static String str = new String("StaticNestedOne");
      static void printName(){
         System.out.println("Class name: "+str);
      }
      static class StaticNestedTwo{
         static String str = new String("StaticNestedTwo");
         static void printName(){
            System.out.println("Class name: "+str);
         }
      }//End of StaticNestedTwo
   } //End of StaticNestedOne
   //An inner class
   class InnerOne{
      public String str1 = new String("InnerOne");
      //An interface within an inner class
      public interface IDPrinter {
         static final int BASE_VALUE=100;
            public void printID();
      }
      public void printName(){
         System.out.println("Class name: "+ this.str1 );
      }
      //Nested inner class that implements an interface
      class InnerNestedOne implements IDPrinter{
         int id;
         protected String str = new String("InnerNestedOne");
         InnerNestedOne(int id){
            this.id = id;
         }
         public void printName(){
            System.out.println("Class name: "+ this.str);
         }
         public void printID(){
            System.out.println(BASE_VALUE+id);
         }
         public void printAllClasses(){
            printName(); //local method
            InnerOne.this.printName(); //enclosing class method
            //creates an object using a private
            //inner class in the outer scope
            InnerTwo innerTwo = new InnerTwo();
            innerTwo.printName();
            OuterMost.this.printName(); //top level method
            StaticNestedOne.printName();
            StaticNestedOne.StaticNestedTwo.printName();
         }
      }// End of InnerNestedOne
   }//End of InnerOne
   // A private inner class
   private class InnerTwo{
      public String str = new String("InnerTwo");
      public void printName(){
         System.out.println("Class name: "+ this.str);
      }
   }// End of InnerTwo
}//End of OuterMost

//A class that uses OuterMost and it's nested and inner classes
class Outside{
   public static void main(String arg[]){

      OuterMost.StaticNestedOne.printName();
      OuterMost.StaticNestedOne.StaticNestedTwo.printName();

      OuterMost outer = new OuterMost();
      OuterMost.InnerOne inner = outer. new InnerOne();
      inner.printName();

      OuterMost.InnerOne.InnerNestedOne innerNested = inner. new InnerNestedOne(10);
      innerNested.printName();
      innerNested.printAllClasses();
   }
}// End of Outside

Listing 4. Extending an inner class outside its enclosing scope.

class InnerExtended extends OuterMost.InnerOne.InnerNestedOne{
   String str = new String("InnerExtended");
   public InnerExtended(OuterMost.InnerOne ione, int id){
      ione.super(id);//creating the inner class instance
   }
   public void printName(){
      System.out.println("Class name: "+ this.str );
   }
   public static void main(String arg[]){
      OuterMost outer = new OuterMost();
      OuterMost.InnerOne inner = outer. new InnerOne();
      InnerExtended ext = new InnerExtended(inner, 100);
      ext.printName();
      ext.printAllClasses();
   }
}

Listing 5: Extending an inner class in a sub class of the top level class.

class OuterExtended extends OuterMost{
   String str = new  String("OuterExtended");
   class InnerOneExtended extends InnerOne{
      String str = new String("InnerOneExtended");
      class NestedExtended extends InnerNestedOne{
         String str = new String("NestedExtended");
         NestedExtended(int id){
            super(id);
         }
         public void printAllClasses( ){
            System.out.println("Class name: "+ this.str);
            System.out.println("Class name: "+
                                InnerOneExtended.this.str);
            System.out.println("Class name: "+
                                OuterExtended.this.str);
            super.printAllClasses();
         }
      } //End of NestedOneExtended
   }// End of InnerOneExtended
   public static void main(String args[]){
      OuterExtended outerE = new OuterExtended();
      InnerOneExtended innerE = outerE. new InnerOneExtended();
      InnerOneExtended.NestedExtended nestedE = innerE .new NestedExtended(200);
      nestedE.printAllClasses();
   }
} //End of OuterExtended

Listing 6: Example of an annonymous class which extends a class.

Frame fr = new Frame();
fr.addWindowListener(
   new WindowAdapter(){
      public void windowClosed(WindowEvent e){
         quit(); //A method in the enclosing class.
      }
   }
);

Listing 7: Example of an annonymous class which implements an interface.

Button dispBtn = new Button("Display");
dispBtn.addActionListener(
   new ActionListener(){
      public void actionPerformed(ActionEvent e){
         displayImage();
      }
   }
);

Listing 8: An event handling example using inner classes.

class FrameMenubar  {
   //Other methods
   private void createGUI(){
      //Other components
      MenuItem about = new MenuItem("About");
      about.addActionListener(new AboutAdapter());
      // other components
   }
   class AboutAdapter implements ActionListener{
      OkBox aboutBox;
      public void actionPerformed(ActionEvent e){
         String topString = new String(copyRight+" "+author+"\n");
         String midString = new String(aboutMidText+"\n");
         //OkBox whose code not shown here is a subclass of
         //java.awt.Dialog. Some variables passed to the
         //OkBox constructor are declared in the enclosing
         //classes.FrameMenuApp is the top-most class which
         //is not shown.
         aboutBox = new OkBox(FrameMenuApp.this,
                              256, // width
                              256, // height
                              aboutBoxTitle,
                              topString+midString,
                              false);
         aboutBox.addActionListener(
            new ActionListener() {//Anonymous class
               public void actionPerformed(ActionEvent e){
                  aboutBox.dispose();
               }
            }
          );
          aboutBox.show();
      }
   }//End of AboutAdapter
}//End of FrameMenubar


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.