A variable is a named memory space
1. Declaration and Assignment method: Data type variable name = value;
The data type is the divided memory space, the variable name is the name of the memory space divided out
2. A variable must be declared before it can be used, not a variable that is not pre-declared and cannot be accessed by a variable that has no value assigned to it.
3. There cannot be a variable of the same name in a scope (that is, between a pair of {} ), because the variable is the name of the memory space (the equivalent of the house number) and the same name will not find the space, so the same name is not allowed.
Scope of the variable: the effective scope of the variable (between a pair of {} ).
4. Variables can be divided into static variables, member variables (variables declared in the class body), local variables (variables declared in the method). The member variable scope is within the entire class body and can reach inside the method but not necessarily (if a variable with the same name is declared in the method, it cannot reach the inside of the method). A local variable scope is only within the function (method) that defines it.
public class var_04{
int i = 10; member Variables
static int m; Static Variables
public void M1 (int m) {// local variable
int k=10; Local Variables
}
public void m2 () {
System.out.println (m);
}
public static void Main (String [] args)
{
var_04 var = new var_04 ();
VAR.M2 ();
}
}
Variables in Java