anonymous classes : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

anonymous classes
A type of inner class that has no name that you define right in the middle of a method (where static init blocks and instance init blocks count as methods). You define it, and create an object of that type as a parameter all in one line. Used for creating simple delegate callback objects. The syntax is strange. It does not use the keywords class, implements or extends. You can refer to this of the outer class via MyOuterClass. this. You can refer to the outer class’s methods by MyOuterClass. this. myOuterInstanceMethod(), MyOuterClass. myOuterInstanceMethod() or simply myOuterInstanceMethod() if there is no ambiguity. Anonymous inner classes are often used in setting up listeners.

These anonymous inner classes can access the static and instance variables of the enclosing outer class. They can also, believe it or not, access a snapshot of the local variables in the method that created the inner class object. To remind you this you are accessing an immutable snapshot, Java insists that the local variables you reference be final. When the class is created, a copy of all the local variables is bundled along with the object for all future references. So in a sense these classes act like closures, taking a little of the environment in which they were created along with them.

Instead of passing arguments to a constructor, your inner class methods can reach out and grab what they need directly from local variables in the enclosing method. The other technique is to use an instance initialiser block. You are only allowed one per anonymous inner class.

The requirement that the enclosing local variables that you access in your anonymous inner class be final is not quite as onerous as it first seems. Just create a new block and create new final local variables in it that copy the non-final ones of the outer block. You can also declare a local variable final inside a loop so long as the value does not change for the rest of the current iteration, even though it will change on the next iteration.

The inner class object may live beyond that of the method that invoked it, so you could not very well have the inner class modifying local variables in the caller that no longer exist. That is why you may only access final local variables. I repeat, you are not really accessing the local variables but a snapshot of them taken at the time the inner class object was instantiated. Anonymous classes are one of the bailing wire constructs in Java.

If you want to baffle those maintaining your code, wags have discovered javac.exe will permit anonymous classes inside static init code and static methods, even though the language spec says than anonymous classes are never static. These anonymous classes, of course, have no access to the instance fields of the object. I don’t recommend doing this. The feature could be pulled at any time.

The big drawback with anonymous classes is they can’t have explicit constructors. You can’t pass them any parameters when they are instantiated. You must rely on the somewhat goofy access to the frozen values of the final temporaries in the invoking method.

Typical Use

Most commonly anonymous classes are used to field events in Listeners.

Learning More



This page is posted
on the web at:

http://mindprod.com/jgloss/anonymousclasses.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\anonymousclasses.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
Blog
IP:[65.110.21.43]
Your face IP:[54.166.10.100]
You are visitor number