As we all know, there are three kinds of variables in the Java class, so what is the difference between the three kinds of variables?
1. Local variables: This is a variable defined within a method. This means that the scope of the variable is within the method. The variable cannot be called in other methods.
2. Member variables: This is a variable that is defined inside the class but outside of the method, and can be instantiated when the object is created. Scopes are internal to the entire class. (including all methods)
3. Class variables: Like member variables, they are defined outside the inner method body of the class. But the difference is that this is a static variable, and plainly it has to be modified with static. Typically used with the class name + variable name, you can not create an object. Greatly improves the efficiency of memory usage.
Next, let's explain the common errors in these three variables.
Local variables cannot be public, private, protected, static, as the name implies that the scope of local variables is within the method, there is no need to use the access modifier. As for the inability to use static, it may be known to the small partners of the JVM that variables inside methods in Java are created by means of stacks. The execution of the method ends and the variables are automatically destroyed. This is inconsistent with static variables.
Variables in the Java class