Say no more, look directly at the code.
Public classMyTest { Public Static voidMain (string[] args) {Test T1=NewTest (); System.out.println ("------------"); Test T2=NewTest (11); System.out.println ("------------"); } }classtest{Private Static intb =Initializeb (); Private Static intA =Initializea (); Private intc =Initializec (); Private Static intInitializea () {System.out.println ("Initialize A In private static method"); return1; } Private Static intInitializeb () {System.out.println ("Initialize B in private static method"); return1; } protected Final intInitializec () {System.out.println ("Initialize C in private method for each object"); return1; } Static //called once at class load time{System.out.println ("C # Static Constructor equivalent\n====static initialization is done===="); } //called every time before the constructor//This is a shared among all the constructors.{System.out.println ("Non-static shared Block"); } PublicTest () {System.out.println ("In the constructor without parameter"); } PublicTest (intuseless) {System.out.println ("In the constructor with only 1 parameter"); }}
Console:
Initialize b in private static method
Initialize A in private static method
C # Static Constructor equivalent
====static initialization is done====
Initialize C in private method for each object
Non-static Shared block
In the constructor without parameter
------------
Initialize C in private method for each object
Non-static Shared block
In the constructor with only 1 parameter
------------
Double Brace Initialization:
See: http://www.c2.com/cgi/wiki?DoubleBraceInitialization
The first brace creates a new anonymousinnerclass, the second declares an instance initializer block which is run when the Anonymous inner class is instantiated. This type of initializer block was formally called an "instance initializer", because it was declared within the instance SC Ope of the class--"static initializers" is a related concept where the keyword static was placed before the brace that s Tarts The block, and which is executed at the class level as soon as the ClassLoader completes loading the class (Specifie D at http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.6) the initializer block can use any methods, Fields and final variables available in the containing scope, but one have to is wary of the fact that initializers is run Before constructors (but not before superclass constructors)
Object initialization in Java (initialization of objects in Java)