Java has 3 types of variables
- Local variables
- Member variables (instance variables)
- class variables (static variables)
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 is declared and must be initialized before it can be used.
Example
package Import_test; public class Dog { public void Pupage () { int age = 1; int color; System.out.println (The age of the puppy is: "+ public static void main (String args[]) {Dog Test = new Dog (); Test.pupage (); }}
Both the age and color in the example are local variables, but color is not assigned, and the system does not assign a value by default, for example:
Package import_test; Public class Dog { publicvoid pupage () { int : Age ; = age + 1; System.out.println ("Puppy's age is:" + ages ); } Public Static void Main (String args[]) { new Dog (); Test.pupage (); }}
Compile error Hint: "Error: (6,") Java: Variable age may not have been initialized
Member variables (instance variables)
- 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.
Example
Packageimport_test; Public classemployee{//This member variable is visible to the child class PublicString name; //private variable, only visible in this class Private Doublesalary; //assigning a value to name in the constructor PublicEmployee (String empname) {name=EmpName; } //set the value of the salary Public voidSetsalary (Doubleempsal) {Salary=empsal; } Public voidprintemp () {System.out.println ("Name:" +name); System.out.println ("Salary:" +salary); } Public Static voidMain (String args[]) {Employee Empone=NewEmployee ("Jim"); Empone.setsalary (2000); Empone.printemp (); }}
Results
name : jimsalary:2000.0
class variables (static variables)
- Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside the method construction method and statement block.
- No matter how many objects a class creates, the class has only one copy of the class variable.
- Static variables are seldom used except when declared as constants. Constants are variables declared as public/private,final and static types. Constants cannot be changed after initialization.
- Static variables are stored in a static storage area. are often declared as constants, and static declaration variables are seldom used alone.
- Static variables are created at the beginning of the program and are destroyed at the end of the program.
- has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types.
- The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, static variables can be initialized in static statement blocks.
- Static variables can be accessed by:classname.variablename .
- When a class variable is declared as public static final type, the class variable name must use uppercase letters. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.
ImportJava.io.*; Public classEmployee {//salary is a static private variable Private Static Doublesalary; //department is a constant Public Static FinalString DEPARTMENT = "Developer"; Public Static voidMain (String args[]) {Salary= 10000; System.out.println (DEPARTMENT+ "Average Salary:" +salary); }}
Execution results
Average developer Salary: 10000.0 developer Average salary:10000.0
Java Learning--variable type