Example:
Packagetest_instance; Public classTestclassloadertime { PublicTestclassloadertime () {System.out.println ("Constructor Execution"); } {System.out.println ("Static code block Execution"); } StaticDemo2 Demo2 =NewDemo2 (); PrivateDemo3 Demo3 =NewDemo3 (); Public Static voidMain (string[] args) {System.out.println ("Main Method Execution"); NewTestclassloadertime (); System.out.println ("Testclassloadertime instantiated"); } }
Execution Result:
Initialization the Demo2 .....
Main method execution
Static code block execution
Initializaiton the Demo3 ....
Constructor execution
Testclassloadertime instantiation of a
So this is done in the following order:
1.demo2 constructor Execution (static property)
2.main Method Execution
3. Static code block execution
4.demo3 constructor Execution (non-static property)
5. Constructor execution
6.TestClassLoaderTime instantiation of
In the Java class, the constructor of the class is executed first, or the private non-static variable of the class is executed first