Member variables and local variables, member variables local variables
1. member variables
1. Description of member variables
The variables defined in the class body are also called member variables (also called attributes or fields ). The member variables are valid throughout the class and are irrelevant to their successive positions defined in the class. That is, member variables can be defined anywhere in the class.
The syntax format of member variables is as follows:
[Modifier] member variable name list of member variable types:
Note: 1. modifiers include public, private, protected, default, static, final, transient, and volatile.
2. The member variable type can be any data type in java, including the basic type and reference type.
3. The name of a member variable is usually named by using a noun. The first letter is lowercase, and the first letter of each word is capitalized to separate each word.
4. If no initial value is assigned to the member variable, java assigns the default value to it, and the default value to the reference type is null.
2. Access to member variables
Static variable is a static variable (class variable), but not a non-static variable (instance variable ). Non-static variables can only be accessed through "object. instance variables". class variables are accessed through "class name. class variables.
2. Local Variables
1. Description of local variables
Local variables mainly exist in the parameter list of methods and the definition of code blocks. The syntax for defining local variables is as follows:
[Modifier] local variable type local variable name list
Note: 1. modifiers can only have final and default. Final indicates that the variable must be assigned an initial value and cannot be modified.
2. the type of local variables can be any data type in java, including the basic type and reference type.
3. The naming rules for local variable names are the same as those for member variable names.
4. It can only be used inside the method and within the code block, and local variables must be initialized before use; otherwise, compilation will fail.
5. when a local variable has the same name as a member variable, the member variable will be hidden in the method and code block of the defined local variable, to use hidden member variables, use the this keyword.