Home : Course Map : Chapter 7 : Java :
Anonymous 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

It is common in Java programming to encounter situations where you need to create an object but don't need to bother giving it an explicit name. For example, to obtain the size of a panel you can use statements like these:

  Dimension d = getSize();
  int width  = d.width;
  int height = d.height;

where a Dimension object is returned from a method and then used to obtain the width and height values. But why bother creating a variable for this since it will never be used again and will only take up memory space.

Instead, you could replace that code with this:

  int width  = getSize().width;
  int height = getSize
().height;

where the compiler keeps track of the Dimension object from which you obtain the width and height.

You can in fact cascade several such calls, using the object returned in the left method to call the next method on the right:

   a = getA().methodB().aMethodC().variableX;

This anonymity eliminates a lot unnecessary named objects and makes it more readable.

With the inner classes we can take this to another level by creating and instantiating a class without bothering to give it a name. In the code below an instance of the ActionListener class is created in the argument of the addActionListener method:

..
public class AnOuterClass extends Applet
{

  Button fBt = new Button("OK");
  public AnOuterClass ()
  {

    
int i = 0;
    fBt.addActionListener
    (
// The argument is the object created by the following:

      new
ActionListener () // no name given to this object
      {
        public void
actionPerformed (ActionEvent e) {
          i++;
          System.out.println ("Pressed "+i+" times");
        }
      }


    
);
    add
(fBt);
  
}
} //
class AnOuterClass

 

Here in one step we created an implementation of the ActionListener interface and created an instance of it for use by the button.

Now the compiler will create a class file name AnOuterClass$1.class where a number, in this case "1", is used to identify the class files for anonymous inner classes.

 

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.