Java basics-member variables and local variables, java member variables
1. Local Variables
In Java, variables defined in classes are called member variables, and their scopes are valid in methods. Local variables must be initialized before they can be used. Otherwise, they cannot be compiled. The following code prompts an error when the local variable is not initialized:
Ii. member variables
1. variables defined in the method are converted into local variables, and their scopes are valid throughout the class. When using a member variable, you do not need to initialize the variable. The system can initialize the variable according to the default principle. The initialization principles are as follows:
No Initialization is required. The Code is as follows:
Note: The final modified member variables must be initialized first:
2. If the local variables defined in a method have the same name as the member variables, this is allowed. In this case, the local variables accessed through the variable name in the method are not member variables.
In the above Code, the print statement outputs 20 instead of 10.