Java Object creation Process

Source: Internet
Author: User

Java Object creation Process

In some projects, a static block is used, and other objects are initialized in the constructor ... A deep understanding of the Java class initialization process, which will not be a problem.

What are the steps of the Java object?

Assuming that there is now a people class and no other classes are shown, the initialization process is like this.

1 The Java interpreter must look up the classpath to locate the People.class file. When the object is first created (the constructor is a static method), or when the static method/static domain of the People class is first accessed.

2 load People.class, all static initialization actions are performed, and static initialization occurs only once when the class object is first loaded.

3 When you create an object using New people (), you will first allocate enough storage space on the heap for the object.

4 Initialize the storage space to binary 0.

4 performs all initialization actions that appear at the property definition.

5 executes the constructor.

See an example:

 Public class Bowl {    Bowl (int  marker) {        System.out.println("Bowl (" + marker + ")");    }     void F1 (int  marker) {        System.out.println ("F1 (" + marker + ")");}    } 

 Public class Table {    staticnew Bowl (1);    Table () {        System.out.println ("table ()");        BOWL2.F1 (1);    }     void F2 (int  marker) {        System.out.println ("F2 (" + marker + ")");     staticnew Bowl (2);}

 Public class Cupboard {    new Bowl (3);     Static New Bowl (4);    Cupboard () {        System.out.println("cupboard ()");        BOWL4.F1 (2);    }     void f3 (int  marker) {        System.out.println ("F3 (" + Marker + ")");     staticnew Bowl (5);}

 Public classstaticinitialization { Public Static voidMain (string[] args) {System.out.println ("Creating New Cupboard () in main"); Newcupboard (); System.out.println ("Creating New Cupboard () in main"); Newcupboard (); TABLE.F2 (1); CUPBOARD.F3 (1); }    StaticTable Table =NewTable (); StaticCupboard cupboard =Newcupboard ();}

The results of the operation are as follows:

The initialization analysis is as follows:

Java Object creation Process

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.