This article tries to make a total of the species of variables to make readers can fully grasp the characteristics of each variable.
Classified according to the declaration method
According to the declaration method, the number of variables in Java has seven, and the following program shows the declaration mode of these seven variables:
Class myclass {
Static Int A;
INT B;
Public static void mymethod (int C) {
Try {
INT D;
} catch (exception e) {
}
}
Myclass (int F) {
Int [] g = new int [100];
}
}
Class Variable: Declare in the Class, outside the Method, and uses static modified variables, such as A above.
Instance variable: Declare in Class, outside the Method, and unused variables, such as B.
Method Parameter: The variables declared in Method parentheses, such as C.
Narrow region variables: declared variables within the Method, such as D and g of the above code code.
Exception-Handler Parameter: A variable declared in Catch parentheses, such as E of the above code code.
Constructor Parameter: A variable declared in the constructor parentheses, such as f of the above code code.
Array Component: The element value of the array does not recognize the name, which must be identified through an array and index (index). For example, G [0] of the above program code.
Classified according to variable memory
According to the position of the variable memory, the variables of Java have two, including:
Heap Variable: The memory is in Heap, which includes Class Variable, Instance Variable, Array Component, A, B, G [0] of the front program. Such variables are automatically initialized by the JVM into a preset value.
Stack Variable: Pan-local variable (Pan-local variable), the memory of the memory is in Stack, which includes narrow zone variables, method parameter, exception-handler parameter, constructor parameter, the front program C , d, e, f. The narrow zone variable will not be initialized by the JVM into a preset value, and the user must initialize the variables themselves, but the Parameter class (including Method Parameter, Exception-Handler Parameter) is initialized by the JVM into a incoming value.
Classified according to the way of use
According to the way of use, as long as it is divided into three classes, it is:
Class Variable: The a in the present example.
Instance variable: that is, B.
Generalized area variable: contains C, D, E, F in the previous example. These four have little difference and are directly attributed to a class.