Originally posted by leo donahue:
I still don't understand what the article means by "copying initializer blocks into every constructor".
Just what it says. The initializer block isn't compiled directly into a method of any kind in the class file the way static blocks are (static initializer blocks are compiled into a static method named "<clinit>", and the JVM calls this method to initialize a class.) Instance initializer blocks are simply copied into each constructor, as you can see here. This class uses three different techniques for initializing instance variables:
If I disassemble it using "javap -c Foo", you see that all three initializations end up looking exactly the same, and they're all in the constructor. Note how the instance initializers come before the constructor code we wrote ourselves, but after the superclass constructor call:
This is cool, a class can run itself without a main method??
Yes. This is a favorite
SCJP mock exam question. "What does this class do when you run it?" Any code in the static blocks runs before the launcher tries (and fails) to find main(). If the static init block contains a call to System.exit(), the launcher will exit before it ever even finds out there's no main() and so no error message will be produced.