Java Local Variables
- A local variable is declared in a method, a construction method, or a block of statements;
- Local variables are created when methods, construction methods, or block of statements are executed, and when they are executed, the variables are destroyed;
- Access modifiers cannot be used for local variables;
- A local variable is only visible in the method, construction method, or block of statements that declares it;
- Local variables are allocated on the stack.
- The local variable has no default value, so the local variable amount is declared and must be initialized before it can be used.
Instance variable
- Instance variables are declared in a class, but outside of methods, construction methods, and statement blocks;
- When an object is instantiated, the value of each instance variable is then determined;
- Instance variables are created when the object is created and destroyed when the object is destroyed;
- The value of the instance variable should be referenced by at least one method, construct method, or statement block, so that the external can obtain instance variable information through these methods;
- Instance variables can be declared before use or after use;
- An access modifier can modify an instance variable;
- Instance variables are visible to methods, construction methods, or block statements in a class. In general, you should set the instance variable to private. By using the access modifier, the instance variable is visible to the child class;
- The instance variable has a default value. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method;
- Instance variables can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.
class variables (static variables)
Fast Intro to Java programming (2)