Can local variables and member variables of the same name?

 Local variables and member variables of the same name, without adding "this" modified, priority use of the most recent variables.

public class Mainceshi {
    private String a = "成员变量";

    public void GetA() {
        String a = "局部变量";
        System.out.println(this.a);
        System.out.println(a);
    }

    public static void main(String[] args) {
        Mainceshi mainceshi = new Mainceshi();
        mainceshi.GetA();
    }
}

print it out

Member variables
local variables

 

JAVA access variables using the principle of proximity, when local variables with the same name member variables, local variables closer, use local variables

java using a local priority ideas. Local variables and member variables can be the same as when using the call identifier, priority use of local variables. Variable names appear in the same instance method, you can use this keyword to call.


Member variables: the variable is declared as an attribute of the class. Static variable (also called a class variable): variable is declared as static properties. Local variables: the variable is declared inside the method. volatile variables: and multithreading related to simplify synchronization variables transient variables: the variables are not for persistent final variable: is substantially constant

 

 

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/91626026