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

Source: Internet
Author: User

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.

First, when will the class be loaded?
Loading when using content in a class: there are three cases
1. Create object: New Staticcode ();
2. Use static members in the class: Staticcode.num=9; Staticcode.show ();
3. Run in the command line: Java Staticcodedemo

Second, the class all content loading sequence and in-memory storage location:
Use statements for analysis.
1.Person p=new person ("Zhangsan", 20);
The things that are done in this sentence:
1. In the stack memory, open the space of main function, build the main function of the variable p.
2. Load class file: Because new uses Person.class, the Person.class class file is first found on the hard disk and loaded into memory.
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. ----> Load period, even if an output statement is not executed.
Static member variable (class variable)-----> Static part of Method area
static method-----The static part of the > method area
Non-static methods (including constructors)-----> non-static portions of the method area
Static code block-----> Static part of Method area
Constructing code blocks-----static parts of the > method area

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.


3. Execute static blocks of code in the class: Initialize the Person.class class, if any.
4. Space: Open space in heap memory and allocate memory addresses.
5. Default initialization: Establishes the unique properties of the object in heap memory and initializes it by default.
6. Display initialization: Displays initialization of properties.
7. Construct the code block: Executes the construction code block in the class and constructs the code block initialization for the object.
8. Constructor initialization: Initializes the corresponding constructor for the object.
9. Assign the memory address to the variable p in the Stack memory.
2.p.setname ("Lisi");
1. In the stack memory to open the SetName method of space, Inside: object reference this, temporary variable name
2. Assigning the value of p to this,this points to the object in the heap that called the method.
3. Assign "Lisi" to the TEMP variable name.
4. Assign the value of the temporary variable to the name of this.
3.person.showcountry ();
1. In the stack memory, open the space for the Showcountry () method, which contains: the reference person for the class name.
2.Person points to the address of the static method area of the person class in the method area.
3. Call 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.
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 block---> Construct code block---> Constructors

Using code to test an example: 06--06:staticcodedemo.java

[Java]View PlainCopy
  1. Class Person
  2. {
  3. private String name;
  4. Private int age=0;
  5. Private static String country="cn";
  6. Person (String name,int age)
  7. {
  8. This.name=name;
  9. This.age=age;
  10. }
  11. Static
  12. {
  13. System.out.println ("Static code block is executed");
  14. }
  15. {System.out.println (name+"..." +age);}
  16. Public void SetName (String name)
  17. {
  18. This.name=name;
  19. }
  20. Public Void Speak ()
  21. {
  22. System.out.println (this.name+"..." +this.age);
  23. }
  24. Public static void Showcountry ()
  25. {
  26. System.out.println ("country=" +country);
  27. }
  28. }
  29. Class Staticdemo
  30. {
  31. Static
  32. {
  33. System.out.println ("Staticdemo Static code block 1");
  34. }
  35. Public static void Main (string[] args)
  36. {
  37. Person p= thenew person ("Zhangsan",100);
  38. P.setname ("Lisi");
  39. P.speak ();
  40. Person.showcountry ();
  41. }
  42. Static
  43. {
  44. System.out.println ("Staticdemo Static code block 2");
  45. }
  46. }



Output Result:
Staticdemo Static code block 1
Staticdemo Static code block 2
Static code blocks are executed
Null... 0//Construction code block
Lisi ... //speak ()
COUNTRY=CN//showcountry ()

The loading order of components in the Java class and the storage location in 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.