[Javase Study Notes]-7.4 Memory loading of Constructors

Source: Internet
Author: User

[Javase Study Notes]-7.4 Memory loading of Constructors

This section describes how constructors are loaded in memory.

As we have said before, constructor is called only once when an object is created. What is the memory change during object creation?

Next we will analyze the Person class in the previous section.

Class Person {private String name; private int age; Person () // constructor, which respectively assigns fixed values to the member variables name and age {name = "baby "; age = 1; System. out. println ("person run");} Person (String n) // constructor, with an initial name parameter {name = n;} Person (String n, int) {name = n; age = a;} public void speak () {System. out. println (name + ":" + age );}}
We use the following two lines of code to analyze the loading process of the constructor in the memory.
Class ConsDemo {public static void main (String [] args) {Person p = new Person ("Xiaoqiang", 10); p. speak ();}}
For the above test, we analyze its running process:

1. The main method goes into the stack memory. The main method has a Person class Variable p;

2. Create a Person object in new and create a space in heap memory (if the address is 0x0045). There are two member variables: name and age;

3. initialize two member variables of the object. At this time, the constructor Person (String n, int a) is automatically called );

4. the constructor Person (String n, int a) goes into the stack memory and has the parameter n = "Xiaoqiang", a = 0;

5. initialize the values of n and a in the heap memory to the name and age variables. At this time, the object initialization is complete;

6. Assign the address 0x0045 to the variable p in the main method;

7. The constructor Person () goes out of the stack and releases the n and a parameters;

8. Execute the p. speak () Statement and call the speak () method in the Person class. Then, the speak method is pushed to the stack;

9. Execute the print statement, jump out of the speak method, and exit the stack using the speak method;

10. jump out of the main method, the main method goes out of the stack, and the program running ends.

We have analyzed a simple object creation process and learned how to load and run constructor in the memory. In this case, object initialization is highlighted, if no constructor is defined in the class, we call the default constructor when creating the object. When we define the constructor, different constructors will be selected for object initialization through parameter types, and we know that all objects must be initialized, and the corresponding constructors will be called during initialization. So, constructors must enter the stack 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.