Java object creation process and memory Running Mechanism

Source: Internet
Author: User

Many people do not know the creation process of an object very well. It is not even clear that many self-proclaimed users who have written more than ten thousand lines of code. I am such a person, after losing money, I learned and studied the whole process again.

 

When the system loads a class and creates an instance of this class, the system automatically allocates memory space for the member variables and automatically specifies the initial value for the member variables after the memory space is allocated. Example:

Create a class test

 

Public classtest {

Private Static Int J = getnumber ();

Public int x = getx ();

Private int A = geta ();

Public void Info (){

System. Out. println ("Info () of test is executed here ()");

}

Protected void infotest (){

System. Out. println ("test protection member test ");

}

Private Static int getnumber (){

System. Out. println ("****** getnumber () function ");

Return 1;

}

Private int getx (){

System. Out. println ("******* getx () method ");

Return 3;

}

Private int geta (){

System. Out. println ("******** geta () method ");

Return 4;

}

}

Create a subclass vtest

Public classvtest extends test {

 

Private Static int A = geta ();

Private int B = getb ();

Private int x = getx ();

Public vtest (){

// System. Out. println ("constructor of vtest ");

}

Private int getb (){

System. Out. println ("---------- THE getb () function is called ");

Return 3;

}

Private Static int geta ()

{

System. Out. println ("------------ T, geta () is called ");

Return 4;

}

Private int getx (){

System. Out. println ("------------ T, getx () is called ");

Return 5;

}

Public void Info (){

Super.info ();

System. Out. println ("the info () function of vtest is executed here ");

}

Public void infotest (){

Super. infotest ();

System. Out. println ("The infotest () function of vtest is executed here ");

}

Public void infotest (Int J ){

System. Out. println ("The infotest (intj) function of vtest is executed here ");

}

Public static void main (string [] ARGs ){

// Todo auto-generated methodstub

New vtest ();

System. Out. println ("***************************");

New vtest ();

System. Out. println ("***************************");

New Test ();

}

 

}

Running result:

* ***** Getnumber () function

------------ T, geta () is called

* ****** Getx () method

* ****** Geta () method

---------- THE getb () function is called.

------------ Getx () in T is called

***************************

* ****** Getx () method

* ****** Geta () method

---------- THE getb () function is called.

------------ Getx () in T is called

***************************

* ****** Getx () method

* ****** Geta () method

 

After analyzing the two classes and the final running results, we can see that:

 

When the program executes new vtest (), because it is the first time to use the vtest class, the system first loads this class and initializes this class. This stage is called the preparation stage of the class. At this stage, the system allocates memory space for the member variables or member constants of the class and specifies the default value. Because the static modifier represents the class itself, they are first loaded and allocated memory space when loading classes. After the class is loaded, the object is created.

Execute New vtest () again. Because the class has been loaded, you do not need to load it again. Here we create an object directly. This is why all of them are new vtest () but the two executions have different results.

So the reader may have another question: Why didn't the static modifier be loaded first when new test is loaded for the first time? This is because Mian () is first loaded in vtest.

 

We often write vtest T = new vtest () in this way. In fact, this statement will execute two processes. The first process creates the object vtest () in the heap memory, second, the variable t of the vtest type is generated in the stack memory, and t stores the address of the vtest object in the heap memory instead of the replica of the entire object. This is another question for the reader. Since the object has been created through new vtest (), why do we still need to declare a vtest t variable, because the object operated on the heap memory can only pass this T. If we don't need this object, we only need to cut off the connection between T and this object, that is, t = NULL. Then, the Java memory recycle mechanism will recycle this memory.

 

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.