|
We have seen in several cases where we can create an object without giving it an explicit name: Dimension
d= getSize(); is replaced by int
width = getSize().width; 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:
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() { Or the even more compact:
tf.addWindowListener(
new WindowAdapter() {
|
||||||||||||
|
|||||||||||||