Java Programming Exercises II
Home Tutorials Reviews Weblog

Review: Java Programming Exercises looks at Anonymous Classes
Feature: One of the problems with secure OO languages like Java is choppiness

A problem for all OO languages like C++, C#, Java, JavaScript, Python, Ruby and others is that in order to bring you encapsulation and security, the coding gets quite choppy. How many Java coders have gotten sick of coding so many private or protected variables with choppy one or two line getters and setters. Ditto for the new ActionScript while C# tries a workaround. And of course GUI, threads, and stack manipulations just add to this chunky, choppy code phenomena.

There are two problems with choppy code. First, it is awkward to maintain with literally hundreds if not thousands of little chunks of methods code. Second, a function invocation and return imposes performance penalties over sequentially running through large pieces of "linear" code. There are a number of work arounds including using String or int arrays to contain a set of properties and then having one setProperty(int which) roll up a whole bunch of getters and setters code into a big switch() statement. This cleans up the coding but may not change the runtime speed.

A second approach is Java's nested anonymous classes. The code below shows in yellow just such

anonymous code used in a small GUI program that implement a Swing JButton in a frame. The problem is the JButton requires a separate ActionListener class to provide the operational method when the button is clicked. We could define this Action Listener Class in a separate file but using a nested anonymous class we can define the new method right where it gets used - in the button.addActionListener() method. Lets follow the code line by line for what is happening here:
1- button.addActionListener( new ActionListener(){
This line invokes a call on method, an addActionListener, which is going to perform the operations when the button(Click Me) is clicked. Normally this is contained in a separate .java file. However, by using the statement new ActionListener(){ I am declaring an anonymous class that is going to be defined right in the invoking code. This works well when the code is short and sweet. Methods of more than 10-15 lines of code or several different class references are better split out One reason: we have had to declare the button final - which means we cannot declare any classes that inherit or derive from this JButton button. But in GUI customizing your components is the name of the game - so be forewarned.
2- public void actionPerformed(ActionEvent evt){
Here is where we define the sole method that will make up this anonymous class. Yes, its just classic Java method code for an ActionListener.
3- button.setText("Hello Dear");
4- button.setBounds(10, 10, 200, 50);
5- System.out.println("We can add in our code here");
6- System.out.println("Perhaps a close database or Save file routine");

And the next four lines of code show that its just ordinary GUI operations. This ActionListener changes the button's text label and repositions and resizes the button as well. However, we do suggest additional operations that can be performed.
7- }
Don't forget to close the brackets on the method;
8- }); // <==Note the button.addActionListener statement ends here
And then to close the anonymous class definition and finally the closing parens for the method close. So remember, with an anonymous class declaration you have three doors (bracket, bracket, parens)to close before you are done. Yes, this is a bit of clumsy coding at the exit; but anonymous classes have the benefit of putting the code right next to where it is used. Note however, because Java generates the anonymous class automatically (it does not inline the code), there is no runtime performance boost.

To the left is a screenshot of what happens with this anonymous class. Note that the buttons text has been changed to Hello Dear. But for some reason, the setBounds method fails to reposition or resize the button. I am not certain why this is occurring - perhaps it is a version difference between Java 1.5 and 1.6 - or its just part of the Java Swing class cross to bear.


Anonymous classes are mostly coding conveniences. Generally they do not improve runtime performance. However, this programmer is a true believer in keeping code together - anonymous classes, despite the stealthy name, help to do just that.

Top of Page  Home  Tutorials 

Developers Weblog  Graphics Reviews and Tips