Java-variable scope (two)

Variables in Java must be defined in a pair of braces, and the code area contained in the braces is the scope of this variable.

  • The global scope can be accessed in the local scope
package JavaClass;

public class _01作用域 {
    
    
    public static void main(String[] args) {
    
    
        {
    
    
            int a=0;
            System.out.println(a);
        }
        System.out.println(a);
    }
}

operation result
Insert picture description here

  • The variable name in the brackets cannot be the same as the variable name in the main function
package JavaClass;

public class _01作用域 {
    
    
    public static void main(String[] args) {
    
    
        int a = 10;
        {
    
    
            int a=0;
            System.out.println(a);
        }
        System.out.println(a);
    }
}

Running results:
Insert picture description here
Java Xiaobaiwang boss guidance

Guess you like

Origin blog.csdn.net/weixin_45666249/article/details/114633014