Anonymous Classes: class or interface : Anonymous Class « Object Oriented « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Object Oriented » Anonymous Class 
6.9.6.Anonymous Classes: class or interface
new SuperClassName(arguments) {
 // Class body
}

Implementing an interface

new InterfaceName() {
 // Implement interface methods
}




import java.awt.*;
import java.awt.event.*;

class MainClass extends Frame {
  public static void main(String[] args) {
    new MainClass();
  }

  MainClass() {
    setLayout(new FlowLayout());
    final Button button = new Button("Click here!");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String label = button.getLabel();
        if (label.equals("Click here!"))
          button.setLabel("Try again");
        else
          button.setLabel("Click here!");
      }
    });
    add(button);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    pack();
    setSize(200200);
    show();
  }
}
6.9.Anonymous Class
6.9.1.Create an anonymous class
6.9.2.Anonymous Class Declarations
6.9.3.You might assign the reference to the constructed object into a variable
6.9.4.Passing Anonymous Class to a method
6.9.5.Passing Arguments into the Construction of an Anonymous Inner Class
6.9.6.Anonymous Classes: class or interface
6.9.7.An instance of an anonymous (unnamed) subclass of MyClass.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.