JAVASE Xiaobai study notes explain the difference between member variables, local variables and static member variables

In Java, we often encounter member variables, local variables and static member variables. For the initial learning of java, you need to make a distinction between the three variables to prevent confusion about the concepts.

variable name Grammatically storage location Life cycle Belonging Defaults transfer
Local variable Variables defined in methods or method declarations (parameters) Stack memory Produced with method call, disappeared with method call completed Belong to method Local variables must be assigned to be used
Member variables Variables defined in the class, outside the method Heap memory Produced as objects are created, disappear as objects disappear Belongs to objects, also called instance variables or object variables Member variables have default values Call by object name
Static variable Defined outside the method in the class, the variable modified by static Static area Load as the class is loaded, unload as the class is unloaded Belongs to a class, also called a class variable Call by class name (Of course, you can also call by object name, but it is not recommended)

The main things to note here are:
When the local variable and the member variable have the same name, the scope of the variable follows the principle of proximity.
The so-called principle of proximity refers to:
first find in a local area (within the scope of the method). If it is found, use it, if it is not found in the local scope, go to the member scope to find it, and use it when found.

//就近原则:先在局部范围找,如果找到就使用。
//如果没有在局部范围找到,就去成员范围去找,找到就使用
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        Star star = new Star();
        //局部变量
        star.show("林俊杰");
    }
}

class Star{
    
    
    //成员变量
    String name="陈奕迅";
    public void show(String name){
    
    
        System.out.println(name);

    }
}

输出的结果为:
林俊杰


Also note that:Variables modified by static are shared variables, shared by all objects of this class

public class MyTest1 {
    
    
    public static void main(String[] args) {
    
    
        Person p1 = new Person();
        p1.name="张三";
        p1.guoji="美国";
        Person p2 = new Person();
        p2.name="李四";
        p2.guoji="英国";
        Person p3 = new Person();
        p3.name="王五";
        p3.guoji="法国";
        System.out.println(p1.guoji);//法国
        System.out.println("===========");
        System.out.println(p2.guoji);//法国
        System.out.println("===========");
        System.out.println(p3.guoji);//法国

    }
}



class Person{
    
    
    //成员变量
    public String name;
    //静态变量
    public static String guoji;
        }

Draw the corresponding memory diagram as:
Insert picture description here

Note: The class information of the current class will be stored in the method area, including the static variables of the class, the initialization code of the class (the assignment statement when defining static variables and the code block for static initialization)

Guess you like

Origin blog.csdn.net/qq_41537102/article/details/110187541