Lecture 8A
  Standalone Java
  Simple Application
  Window App
  Applet to App 1
  Applet to App 2
  Applet Viewer
  Inner Classes
  
AnnonymousClass
  AdapterClass
 
Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 8A : Anonymous Classes
Anonymous Classes

We have seen in several cases where we can create an object without giving it an explicit name:

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

is replaced by

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

where the compiler keeps track of the Dimension object that we obtain width and height from.

We 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 from the code a lot unnecessary named objects and makes it more readable.

With the inner classes we can take this another step by creating and instantiating a subclass without bothering to give it a name:

..
public class outer extends Applet{
  int i=0;
  Button bt = new Button("OK");
  public outer(){

    bt.addActionListener( new ActionListener(){
      public void actionPerformed(ActionEvent e){
        i++;
        System.out.println("Pressed "+i+" times");
      }
    }

    );
    add(bt);
  }
}

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 outer$1.class

We now see how we used an anonymous WindowAdapter subclass in the earlier applications to catch the windowClosing events and properly exit from the application:

    WindowListener listener = new WindowAdapter() {
      public void windowClosing(WindowEvent e){
        System.exit(0); }
    };
    // and add it to the frame.
    tf.addWindowListener(listener);

Or the even more compact:

    tf.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e){
        System.exit(0); }
    } );

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey