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
- 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:
Java Xiaobaiwang boss guidance