Java variable scope

/*
1, has a variable priority, if there is a local variable in the method, the read priority local variables,
If no local variables in a method, outside the body of the class, the method has member variables, member variables is read
Commonly known as the [principle of proximity]
 
2. What is the scope?
1. Scope of variables, mainly described is effective variable range;
2. Within the scope of what can be accessed, as long as the variable is out of this range can not access.
3. declare member variables, to be added "static" in front of the data type!

3, on the classification variables:
According to the classification of the position of variable declarations:
1. Local variables:
- variable method which is called a local variable declaration
2. The member variables:
- [] within the body of the class in vitro method member variable called variable declaration

4, - in which a different scope, the variable name is the same, but if the method has a local variable, then the local variable priority read,
If no local variables in a method, outside the body of the class, the method has member variables, member variables is read
- in the same role in which variable names must be unique.

5, in class, you can not directly write Java variable declaration statement [except]
 
6, in a statement variables, local variables must be initialized and assigned; member variable must be initialized, but can not be assigned. Because the member variables in Java if you do not assign default to 0;
Such as: int, short, byte, long 0;
boolean false (true:1,false:0)
folat double 0.0
char \u0000
*/
public class Sa{
//Member variables
static String x = "hhhhhhsffjfjf"; // member variable, the braces can not be called
// main method, the inlet
public static void main(String[] args){
// a scope in the main method, in addition to the main method can not be called.
int a = 10; // local variables, in addition to braces, can not be called
System.out.println(a);
System.out.println(x);

}
}
In the above study notes are beep beep miles miles summary video lesson in the power node, if wrong, please point out! ~

Guess you like

Origin www.cnblogs.com/zhuojinyong/p/11267478.html