In the Java class, the constructor of the class is executed first, or the private non-static variable of the class is executed first

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.