Shielding principle variables of the same name

Java variables follow the "principle of the same name variables shield", please read the relevant information on after-school knowledge to figure out, and then write some test code yourself, as this example, like, consciously define some variables of the same name in different places and see the output in the end which value.

 

 

Conclusion: Each variable has an effective area, i.e. that area defined out of this area, the variable is no longer valid. (Except for global variables)

chestnut:

package test;

public class Test {
private static int Albert = 1;
public static void main(String[] args) {
int Albert = 2;
System.out.println(Albert);
}
}

result:

2

Guess you like

Origin www.cnblogs.com/shenaoyu/p/11541236.html