There are three types of Java variables:
- Local variables (local variables): Created at method invocation, destroyed at end of method
- instance variables (global variables): Created when class is created, destroyed when class is destroyed
- Class variable (static variable): program startup is created, destruction is destroyed when program is destroyed
1 Public class variable{2 Static int allclicks=0; // 3 String str= "Hello World"; // instance Variable 4 Public void method () {5 int i =0; // Local Variables }7}
Local 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.
In the following instance, age is a local variable. Defined in the Pupage () method, its scope is limited to this method.
1 Public classpuppy{2 Public voidPupage () {3 intAge = 0;4Age = Age + 7;5System.out.println ("Puppy's age is:" +Age );6 }7 Public Static voidMain (String []args) {8Puppy Puppy =NewPuppy ();9 puppy.pupage ();Ten } One}
Where age must first initialize, if not initialized when the compilation will be an error.
1 Public classpuppy{2 Public voidPupage () {3 intAge ;4Age = Age + 7;5System.out.println ("Puppy's age is:" +Age );6 }7 Public Static voidMain (String []args) {8Puppy Puppy =NewPuppy ();9 puppy.pupage ();Ten } One}
Operation Error:
Error: (+) Java: Variable age may not have been initialized
Instance variable
- An instance variable is declared in a class, but it is outside the method, constructor, or block.
- When an object in the heap is allocated a space, the location of each instance variable is created.
- When the object is created with the keyword "new", the instance variable is created, and it is destroyed when the object is destroyed.
- The value of an instance variable must be referenced by more than one method, constructor, or block, or an important part of the state of the object that must appear in the class.
- Instance variables can be declared at class level before use or after use.
- An instance variable can be accessed with an accessible descriptor.
- Instance variables are visible to all methods, constructors, or blocks in a class. In general, these variables are recommended to be private (access level). However, the visibility of subclasses can be given to those variables by accessible descriptors.
- The instance variable has a default value. The default value for the number is zero, the default value for the Boolean is false, and the default value for the object reference is null. Assignments can be made within a declaration or constructor.
Note : the instance variable can be defined as final, and the variable defined as final cannot be modified.
Instance variables have public type and private type (private).
1 Public classemployee{2 //This instance variable is visible to the child class3 PublicString name;4 //instance variable, private variable, only visible in the class5 Private intsalary;6 //constructor assigns a value to name7 PublicEmployee (String empname) {8Name =EmpName;9 }Ten //set the value of the salary One Public voidSetsalary (intempsalary) { ASalary =empsalary; - } - //Printing Information the Public voidEmpprint () { -System.out.println ("Employee ' s name is:" +name); -System.out.println ("Employee ' s Age is:" +salary); - } + - Public Static voidMain (String args[]) { +Employee Empone =NewEmployee ("Jacky"); AEmpone.setsalary (5000); at empone.empprint (); - } -}
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 .
- Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.
1 Public classpuppy{2 //Age is a static private variable3 Private Static intAge ;4 //animal is a constant5 Public Static FinalString ANIMAL = "Puppy";6 7 Public Static voidMain (String args[]) {8Age = 6;9The Age of System.out.println (animal+ "is:" +Age );Ten } One}
Run output:
The age of the puppy is: 6
If other classes want to access the ANIMAL constant, you can access this: Puppy.animal.
1 Public classtest{2 Public Static voidMain (String args[]) {3 System.out.println (puppy.animal);4 }5 }6 classpuppy{7 //animal is a constant8 Public Static FinalString ANIMAL = "Puppy";9 Ten}
Run output:
Little Dog
Note: Instance variables must be created before they can be used by the object, and static variables can be referenced directly using the class name.
1 Public classpuppy{2 //static variable Age13 Public Static intAge1 = 11;4 //instance variable Age25 Private intAge2 = 22;6 7 Public Static voidMain (String args[]) {8 //static variables can be used directly in the class9System.out.println ("Age1:" +Age1);Ten One //instance variables need to be instantiated before they can be used APuppy Puppy =NewPuppy (); -System.out.println ("Age2:" +puppy. AGE2); - } the}
Run output:
Age1:11Age2:22
Java Learning Note (vi): variable type