« When Only An Example Will Do | Main | Well Behaved Rules »

Trivia

Listen to this articleListen to this article

I've just found out that Amazonian women cut off their right breast so as to be able to use the bow better. Guess I'll stop lusting after Amazonian women from now on!

In other news, an old friend of mine, Mike Lee, was down from Sydney today and passed on a piece of obscure Java knowledge.

He had spent some considerable time debugging a junior developers code, trying to work out what was going on with a static initialiser. He couldn't work out why it was executing just prior to, and every time, the constructor of the class being called.

Eventually he worked it out. The developer had mistakenly omitted the static keyword from the initialiser block.

Well blow me down, Java has anonymous constructors (well that's the name I've given them at least)! Yup, that's right, this is perfectly legal Java:

class Foo {
    private final String _bar;

    {
        _bar = "Hello, World";
    }
}

In fact, just like static initialisers, you can declare any number of anonymous constructors and they will all be run (in declaration order) prior to invoking a "regularly" declared constructor.

So, to all you JLS weenies out there who already knew this, you don't deserve to get a life. For the rest of us mere mortals, WTF?! :-)

Comments

I must admit that I knew this, although I haven't found a real use for it.

Meanwhile we can enjoy this cute hack combining anonymous class and instance initializer. (Not for those with weak stomach.)

public class Foo {
private int a;
private int b;
private int c;

public static Foo createFoo() {
return new Foo() { { a = 1; b = 2; c = 3; } };
}
}

These are technically called "instance initializers" (J2LS section 8.6), but Simon's name (anonymous constructors) is probably a better description. They were added to Java when anonymous classes were added. It isn't possible to code a constructor for an anonymous class; what would the constructor's name be? So the instance initializer was provided to allow instances of anonymous classes to be initialized.

Juha's example isn't really a hack, it's exactly what the instance initializer was designed for. Except... the way the statement is formatted will probably throw a lot of people. This was recently mentioned in the Euxx weblog: http://jroller.com/comments/eu/Weblog/double_curly_brackets

The examples in the language specification show it being used to combine initialisation that's common to all constructors. For example, you may want an instance to register itself somewhere, and an instance initialiser can be used rather than reproducing the call in each constructor.

Obviously, in this case, the value is dependant on the number of constructors that you have.

Note that it most definitely is not a constructor; it actually gets called prior to the appropriate constructor.

...fair call, but I guess that it's better to lose a boob in order to shoot straight (as opposed to the usual reason women have radical mastectomies.)

"Everyone knows the story of how the Amazons cut off their right breasts so they wouldn't interfere with the use of the bow. Trouble is, the story is a crock--an old crock, but a crock nonetheless."

http://www.straightdope.com/mailbag/mamazon.html

Actually Mike and I found this because Checkstyle has the ability to restrict the number of statements allowed inside an intialiser block, and we had too look up the JLS to find out what that was.

We're setting the limit to "0" - I'm pretty sure no one has ever found a genuine practial use for one. And even if they have, they're going to confuse the heck out of everyone else.

The anonymouse class business make sense, but if you really need to do it, just name your class - everyone will understand much more easily.

Post a comment