Usage of static modified member variables and modified member methods

static modified member variables

Member variable classification

Member variables of a class are divided into two types according to whether they have static modification: class variables and instance variables (object variables)

  • Class variable: has static modification, belongs to the class, has only one copy in the computer, and will be shared by all objects of the class
  • Instance variables: no static modification, belonging to each object, each instantiated object does not affect each other
/*
    作者:絮纸为花
*/
public class Student{
    //类变量,所有实例化对象都用这一个
    static String name;
    //实例变量,每个实例化对象都有自己的,互不影响
    int age;
}
  • Class name. Class variable (Using this calling method, you can clearly know that this variable is a class variable, because instance variables can only be accessed using: object. instance variable. The same is true for instance methods.)
  • Object.Class variable (easy to confuse)

How member variables work

Stack, heap, method area, we think of memory as these three parts.

When executing the main function, the written class will be placed in the method area for execution. The class containing the main function will also be placed in the method area for execution. The variables in the class we have written are also in the method area.

If there is a class variable, this class variable will be placed in the heap area.

Next, the main function body is executed, and the instantiated object we created will be placed in the stack area for execution.

Modify class variables

If we change class variables by instantiating objects, the order is,

Stack (instantiated object) -> Heap (yes, but the instantiation cannot be found) -> Method area (the instantiated object is reported in the superior of the method area, and the superior has the address of the class variable in the heap area, find it, and modify it)

If you modify it through the class name, start directly from the method area.

Modify instance variables

Stack (instantiated object) -> Heap (yes, modified directly)

Application scenarios

Only one copy of a certain data can be shared, accessed, modified, and class variables can be used

Every instantiated object must have it, using instance variables.

static modified member method

Similarly, it is almost the same as static modified member variables.

The difference is that class methods are not placed in the heap area, but are placed together with the class in the method area. The method area, as the name suggests, is the memory partition where methods are stored.

Therefore, when accessing class methods through instance objects, you only need to use the method area to find them.

If you look carefully, you can find that the main method is also a class method.

java class name——》class name.main to run

The main method can also accept parameters. In cmd, after compilation, at runtime, the java class name parameter will be passed to main. You can traverse the String array to access the parameters you passed.

Class method application scenarios: as a tool class (methods written in advance, such as certain algorithms and data structures)

Tools: Improve reusability and save memory.

If you write a utility class, you should make your utility class constructor private to avoid accidentally creating objects.

static precautions

  • Class methods can directly access class members, but cannot directly access instance members.
  • In instance methods, you can directly access class members and instance members.
  • The this keyword can be used in instance methods, but the this keyword cannot be used in class methods.

The main idea: Classes are templates, instances are products, there are templates first and then there are products.

The product uses the template and itself, but the template cannot use the product, and the template can only use the template itself.

code block

Code block is one of the five major components of a class (member variables, constructors, methods, code blocks, inner classes)

There are two types of code blocks:

Static code block:

  • Format: static{ }
  • Features: Automatically executed when the class is loaded. Since the class is only loaded once, the static code block is also executed once.
  • Function: Complete the initialization of the class, official tool class initialization is multi-purpose

Example code block:

  • Format: static{ }
  • Features: Each time an object is created, the instance code block is executed and executed before the constructor
  • Function: Same as the constructor, completes the initialization of the class,

Guess you like

Origin blog.csdn.net/weixin_62302176/article/details/132517444