A brief discussion on the loading order and storage location of each component in Java class

  • 2020-06-12 09:14:49
  • OfStack

1. When will the class be loaded?

Load when using content in a class: There are three cases

1. Create objects: new StaticCode();

2. Use static members of the class: StaticCode. num=9; StaticCode. show ();

3. Run java StaticCodeDemo from the command line

2. Load order and location of all contents of the class in memory

Use statements for analysis:

1.Person p=new Person("zhangsan",20);

What it does:

1. Open up the space of main function in the stack memory, and establish the variable p of main function.

2. Load the class file: Since new USES Person.class, first find the Person.class class file from the hard disk and load it into memory.

When a class file is loaded, everything is loaded except for non-static member variables (properties specific to the object).

Remember: Load puts 1 line of the class file in memory and does not execute any statements. ---- > At load time, even if there is an output statement, it is not executed.

Static member variables (class variables) -- > The static part of the method area

Static method -- > The static part of the method area

Static code block -- > The static part of the method area

Non-static methods (including constructors) -- > The non-static part of the method area

Construct code blocks -- > The static part of the method area


When the Person.class file is loaded, both static and non-static methods are loaded into the method area, except that one object needs to be instantiated to call the non-static method.

Object to call a non-static method. If all the non-static methods in a class were created once with an object instantiation, it would 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 the object calling the non-static method.

3. Execute a static block of code in a class: Initialize the Person.class class, if any.

4. Create space: Create space in heap memory and allocate memory address.

5. Default initialization: Create specific properties of objects in heap memory and initialize them by default.

6. Display initialization: Display initialization for properties.

7. Construction block: Executes the construction block in the class and initializes the construction block to the object.

Constructor initialization: Initializes the corresponding constructor for the object.

9. Assigns a memory address to the variable p in stack memory.


1. Create space in stack memory for setName methods, including: object reference this, temporary variable name

2. Assign the value of p to this, which points to the object in the heap calling the method.

3. Assign "lisi" to the temporary variable name.

4. Assign the value of a temporary variable to this's name.


In stack memory, showCountry() method space, which contains: class name reference Person.

Person points to the address of the static method area of the Person class in the method area.

3. Call country in the static method area and output.

Note: To use a member of a class, you must call. By what call? There are: class name, this, super

3. The difference between static code blocks, construction blocks, and constructors

Static code block: Used to initialize a class, the class is loaded and executed once.

Construction code block: used to initialize an object. This part is executed as soon as the object is created, and takes precedence over the constructor.

Constructor: Initialize the corresponding object. When creating the object, select the constructor to initialize the object.

When the object is created, three are loaded in the order of execution: the static code block -- > Construct the code block -- > The constructor

class Person 
  private String name; 
  private int age=0; 
  private static String country="cn";  
  Person(String name,int age) 
    System.out.println(" Static blocks of code are executed ");  
  { System.out.println(name+"..."+age);  } 
  public void setName(String name) 
  public void speak() 
  public static void showCountry() 
class StaticDemo 
    System.out.println("StaticDemo  Static code block 1");   
  public static void main(String[] args) 
    Person p=new Person("zhangsan",100); 
    System.out.println("StaticDemo  Static code block 2");   

Output results:

StaticDemo static code block 1
StaticDemo static code block 2

Static blocks of code are executed

null... 0 // Construct code blocks
lisi...100 //speak()
country=cn //showCountry()

Related articles: