Anonymous classes are useful for many situations. They are, in the most basic level, an inner class created by the compiler, implementing an abstract class or interface inside a method body. I’ll try to give some information about how this feature is implemented and some pointers along the way as to how to best use it when writing Java code.
An Example
Even though this entry is not to going to teach about the syntax and semantics of writing anonymous classes, an example is needed for the post to reference to. So, imagine the need for a “lazy indexer”. This indexer supports getting values for pre-defined keys, but instead of actually containing these values in a hash map, it allows external code to retrieve the values for it. This could be useful to hide a data store behind a Map interface, without retrieving and mapping into objects all the database values beforehand.
So, the lazy indexer will have the following interface available for the user to implement:
public interface ValueRetriever<K, V> {
V getValueForKey(K key);
}
The lazy indexer, in its turn, is defined (partly) like this:
public final class LazyIndexer<K, V>
implements Indexer<K, V> {
public LazyIndexer(ValueRetriever<K, V> retriever) { .. }
public V get(K key) {
return retriever.getValueForKey(key);
}
}
So, in order to use the indexer, one needs to implement the ValueRetriever interface. For example, the following method should retrieve an indexer which maps order serial numbers to order objects: Indexer<Integer, Order> getOrders();. If the method retrieves the data from the database, it might want to use that connection already made, and the data already retrieved, to implement the getOrders method.
public Indexer<Integer, Order> getOrders() {
final OrderQueries orderQueries = conn.createQueryObject(OrderQueries.class);
ValueRetriever<Integer, Order> orderRetriever =
new ValueRetriever<Integer, Order>() {
public Order getValueForKey(Integer key) {
return orderQueries.selectOrder(key);
}
};
return new LazyIndexer<Integer, Order>(orderRetriever);
}
The compiler will make sure to create a class to implement the ValueRetriever interface. Notice the use of the final keyword for the orderQueries variable, as it is mandatory. As you could see here, this is one example of when to use anonymous classes: They can be used to replace what was commonly known in C as method pointers, and in C# as delegates. Unlike method pointers, however, they are completely object-oriented, and unlike delegates, they do not use reflection of any kind – A new class is created, and direct calls to methods are being performed.
As this post is getting longer than I had expected, I will have to continue it in a follow-up post. The next post will look into what it is exactly the compiler creates for you, and why the final keyword was necessary. It will also look into more uses and practices with anonymous classes. Hope you enjoyed this post enough to read the next one!
PS,
I want to apologise again for the reduced post rate recently. I guess real life took its toll, and I do hope that soon I will be able to increase the pace and also get back to working on Xml2Java, my XML binding project.
Liked Chaotic Java? It's free! But I also make some other things that aren't, which you might like. Like Firewall, a rule changing, turn based strategy game for iOS.
June 3rd, 2006 at 9:51 pm
Oops, i discovered your blog three ” sites ” ago. Always interested in a good Java blog but I see no recent entries. What a mess you made of it. Why not move to the next? What’s wrong with blogspot.com anyway?
June 4th, 2006 at 7:50 am
Hello Jim!
I’m sorry you were disappointed from the Java blog – I haven’t posted in a while due to personal issues, which I hope be resolved by October. It saddens me as well that these issues are not resolved as fast as I hoped it would have been.
The switch to its own domain was made when things were still looking pink for the site – I hope that when I return to writing it would be more useful. Again, sorry for you having to skip three sites in order to reach this one.
Thanks for your interest, though. I hope to “see” you here in a few months!
July 7th, 2006 at 10:18 am
Hi,
Looking forward to you start blogging again… Your blog is very educational and thoroughly. A pleasure to read!!
I hope you get your personal issues solved and looking forward hearing from you again!
Kim