Inner Anonymous Classes

It's possible to go one step further from a local inner class to something called an anonymous class. An anonymous class is a refinement of inner classes, allowing you to write the definition of the class, followed by the instance allocation.

The anonymous class is explained in Table 12-4.

Table 12-4. Anonymous class

Java term

Description

Example code

Anonymous class

This is an inner class.

It is a variation on a local class.

void foo () {

  JFrame jf = new JFrame();
  jf.addEventHandler(
     new EventAdapter() {
         myOverridingMethod(){
         /* some code */
         } // end method
    } // end anon class
  );
}

The class is declared and instantiated within a single expression.

Qualities of an inner anonymous class

Instead of just nesting the class like any other declaration, you go to the

new SomeClass ()

part of the statement where an object is instantiated, and immediately follow it with a block containing the class body, such as the following:

{
     public void actionPerformed(ActionEvent e) {
         System.out.println(e.paramString() + " pressed");
     }
 }

We have already seen something like this before. Think back to the constant-specific class bodies of enums. Those could hold a method that overrides a method in the base enum. The same kind of thing is going on here. The full example is shown in Figure 12-3.

Figure 12-3. An anonymous class.

image

Where to use an inner anonymous class

This is quite specialized. The block that you provide is held to be an anonymous (no-name) class that extends the class or interface name. The whole purpose is that you provide overriding versions of one or more of the methods in the base class. If SomeClass is an interface, you have to provide all the methods, of course.

It's an even more concise way of writing an event handler than a local class. With a local class you have to provide all method bodies. With an anonymous class, you cram it into the instantiation and only provide method bodies for the methods you want to change.

You never have to use any kind of an inner class. You can always get the same effect with top-level classes. Inner classes are intended as conveniences to make certain common coding constructs quick to write.

Example inner anonymous class

Try compiling and running this example.Your f2.java file generates class files called f2.class and f2$1.class. The second of these represents the anonymous ActionListener inner class.

Be sure you are clear on what is going on here, even if the code doesn't make total sense until we reach Chapter 20. This is saying that there is an Interface (or class—we can't tell from looking at the code here) called ActionListener. We are declaring an object of an anonymous class type that either implements the interface or extends the class. If it extends the class, then any methods we define override the corresponding methods of the base class.

You should only use inner classes and anonymous classes where the event handler is just a few lines long. If the event handler is more than a screenful of text, it should be in a named top-level class. We have to admit; however, that the notational convenience for smaller cases is considerable. Just don't get carried away with it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223