Classification of variables in Java-local variables, instance variables, static variables

1. Classification of variables in Java

    Variables in Java can be divided into local variables and member variables according to different declaration positions . If a member variable is modified with the static keyword, it is also called a static member variable . If it is not modified, it is also called an instance member variable . The following describes the connections and differences between the three of them.

public class Variable {
    
    
    //静态成员变量(静态变量)
    static int x=10;
    //实例成员变量(成员变量)
    int y=20;

    public static void main(String[] args) {
    
    
        //局部变量
        int num=30;
    }
}

2. The difference between local variables, member variables, and static variables

variable Declare location Memory location Life cycle initialization
Local variable In the method or on the method declaration (parameters) Stack Created as the method is called, disappear as the method disappears Initialization must be assigned
Instance variable Outside method in class heap Created as objects are created, disappear as objects disappear Has a default initialization value
Static variable Outside method in class Static area Load as the class is loaded, and disappear as the class disappears Has default initialization

When to use static variables?

When a certain "attribute value" of all objects of a class does not change with the change of the object, in order to save heap memory, we can define the attribute as a class-level attribute, that is, declare it as a static variable, and increase to "Entire family" data, such variables can be accessed directly using "class name" without creating objects.

Member variables Surface difference scenes to be used Call method Memory location
Instance variable No static modification For every object in the class isUnique attributes Can only be called by object name Heap area
Static variable Modified by static For each object in the classConsistent attributes You can use the class name to call, you can also use the object name to call (not recommended) Static area

For example:

public class ManTest {
    
    
    public static void main(String[] args) {
    
    
        Man man=new Man("小明");
        //实例变量使用对象名调用
        System.out.println("名字"+man.name);
        //静态变量可使用类名调用(推荐)
        System.out.println("性别"+Man.sex);
        //静态变量也可以通过对象名调用(不推荐)
        System.out.println("性别"+man.sex);
    }
}
class Man{
    
    
    //实例变量
    String name;
    //静态变量
    static char sex='男';
    Man(){
    
    }
    Man(String name) {
    
    
        this.name = name;
    }
}

A few notes

  1. The principle of proximity
      when members of the same local variables and variable names, use in the method, using the principle of proximity.
  2. Scope
      Variable names in the same domain cannot have the same name, and different domain variable names can be the same. Java follows the principle of proximity and will automatically access the nearest data.
    Regarding the valid range of variables, let us remember one sentence: you won’t recognize it after the braces .

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/109995487