The loading order of the components in the Java class is reproduced in the memory location-----

Source: Internet
Author: User

1 in a written exam, there is an issue about static blocks of code for classes, building blocks of code, and the order in which constructors are executed. Not too clear, Baidu on the internet a bit. Keep a record here. 2 3 first, when will the class be loaded? 4 loading when using content in a class: There are three cases51. Create the object:NewStaticcode ();62. Using static members in a class: Staticcode.num=9; Staticcode.show ();73. Run in the command line: Java Staticcodedemo8 9 second, the class all content loading sequence and in-memory storage location:Ten use statements for analysis.  One1.Person p=NewPerson ("Zhangsan", 20); A the things that are done in this sentence: -1in the stack memory, open the space of the main function and create the variable p for the main function.  -2. Load the class file: Because new will use person.class, you will first find the Person.class class file from your hard disk and load it into memory.  the when loading a class file, other than the non-static member variable (the object's unique properties) will not be loaded and the rest will be loaded.  -Remember: Loading is a row of rows in a class file that is stored in memory and does not execute any statements. ---->the load period is not executed even if there are output statements.  -Static member variables (class variables)----->static part of the method area -static method----->static part of the method area +Non-static methods (including constructors)----->non-static part of the method area -Static code block----->static part of the method area +Constructing code Blocks----->static part of the method area A  at Note: Both static and non-static methods are loaded into the method area when the Person.class file is loaded, except that an object needs to be instantiated before calling to a non-static method - , the object can call a non-static method. If you let all non-static methods in a class build up once with the instantiation of the object, you consume a lot of memory resources. - That 's why all objects share these non-static methods, and then use the This keyword to point to an object that calls a non-static method.  -  -  -3The static code block in the execution class: initializes the Person.class class, if any.  in4Open Space: space in heap memory, allocating memory addresses.  -5. Default initialization: Establishes the unique properties of the object in heap memory and initializes it by default.  to6. Display initialization: Display initialization of properties.  +7constructs a code block: Executes the construction code block in the class and constructs the code block initialization for the object.  -8Constructor Initialization: Initializes the corresponding constructor for the object.  the9The memory address is assigned to the variable p in the Stack memory.  *2.p.setname ("Lisi"); $1The space for the SetName method in the Stack memory: object reference this, TEMP variable namePanax Notoginseng2The value of P is assigned to This,this, which points to the object in the heap that called the method.  -3. Add "Lisi"assigns a value to the TEMP variable name.  the4assigns the value of the temporary variable to the name of this.  +3. Person.showcountry (); A1in the stack memory, open the space for the Showcountry () method, which contains: the reference person for the class name.  the2. Person points to the address of the static method area of the person class in the method area.  +3call the country in the static method area and output.  -Note: To use a member in a class, you must call it. By what call? There are: class name, This、Super $    $ three, static code block, construct code block and constructor function difference - Static code block: Used to initialize the class, when the class is loaded, it is loaded and executed only once.  - constructs a code block: Used to initialize an object. This part is executed as long as the object is created, and takes precedence over the constructor.  the constructor: When an object is initialized for the corresponding object, the corresponding constructor is selected to initialize the object.  -When an object is created, the three are loaded in the execution order: static code blocks---> Building blocks of code--->constructor FunctionWuyi   the //using code to test an example: 06--06:staticcodedemo.java - [Java] View plain copy Wu class Person - {   About PrivateString name;  $ Private intAge=0;  - Private StaticString country= "cn";  -Person (String name,intAge ) - {   A  This. name=name;  +  This. age=Age ;  the }   - Static   $ {   theSystem.out.println ("Static code block is executed");  the }   the{System.out.println (name+ "..." +Age );}  the  Public voidsetName (String name) - {   in  This. name=name;  the }   the  Public voidspeak () About {   theSystem.out.println ( This. name+ "..." + This. Age);  the }   the  Public Static voidshowcountry () + {   -System.out.println ("country=" +country);  the }  Bayi }   the classStaticdemo the {   - Static   - {   theSystem.out.println ("Staticdemo Static code block 1");  the }   the  Public Static voidMain (string[] args) the {   -Person p=NewPerson ("Zhangsan", 100);  theP.setname ("Lisi");  the P.speak ();  the Person.showcountry (); 94 }   the Static   the {   theSystem.out.println ("Staticdemo Static code block 2"); 98 }    About }   - 101 102 Output Result:103 Staticdemo Static code block 1104 Staticdemo Static code block 2 the static code blocks are executed106  NULL... 0//Building Code Blocks107Lisi ... 100//speak ()108Country=cn//showcountry ()

The loading order of the components in the Java class and the storage location in memory-----reproduced

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.