On page 202 - 203 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that is supposed to give output based on the order of initialization:
Until now, I have never seen print statements like those. We're not in the main method (or any method acually), so how is the the print statement in the block on line 6 supposed to work? And what are those nameless static blocks on lines 10 - 16?
The book says the output is supposed to be:
But the way my code is now, it doesn't print anything. What is going on?
Please be careful about thread names; I have looked for bizarre print statements and didn't find any, and I looked for print statements producing no output, and I didn't find any of them either . I think this is a different version of the same problem you encountered here.
posted 4 weeks ago
I don't think you will find much about initialisers in the Java™ Tutorials. You can try the Java® Language Specification (=JLS) (also this JLS section), but I would have thought that your book would provide a better description of what initialisers look like.
Nameless initializer blocks allow complex initialization of classes and class instances independent of constructors.
Effectively, nameless static initializers are executed in the order that they appear when the class is first loaded. Nameless non-static initializers are processed similarly except that in effect they are all bundled up into an invisible pre-constructor method that gets invoked before the actual constructors (if any) start processing. Or, if there are no constructors, when an object is instantiated.
Being persecuted doesn't in any way prove your righteousness or your beliefs. Many people get persecuted because they are repugnant or annoying. Or just because they can be.
posted 4 weeks ago
Doesn't that mean, after the superclass' constructor has completed and before any code in the current object's constructor(s) is executed?