McWalter.org :: Weird java tricks


Here are some tricks one can do in java, some weirder than others.

Non-default pseudoconstructors for anonymous classes

Anonymous classes can't have constructors. Since java 1.1 it's been possible use instance initializers, which function like default constructors. This technical note shows a little trick that provides an effective equivalent to non-default constructors. More information here.

External references to anonymous classes

Generally, one can't instanciate members of an anonymous class anywhere except at its local definition. Even although there's absolutely no reason to do so, this is a restriction one can workaround if one tries hard - this technical note shows how.