Variable Types in Java

  • 2021-12-11 17:42:12
  • OfStack

Directory 1, local variable 2, member variable (instance variable) 3, class variable

1. Local variables

Variables defined in a method or statement block are called local variables. Variable declaration and initialization are both in the method, and after the method ends, the variable will be destroyed automatically.

Local variables are declared in methods, constructors, or statement blocks; Local variables are created when a method, constructor, or block of statements is executed, and when they are executed, the variables are destroyed; Access modifiers cannot be used for local variables; A local variable is visible only in the method, constructor, or statement block that declares it; Local variables are allocated on the stack. Local variables do not have default values, so local variables must be initialized before they can be used.

2. Member variables (instance variables)

A member variable is a variable defined in a class outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods in classes, constructors and statement blocks of specific classes.

Instance variables are declared in 1 class, but outside methods, constructors and statement blocks; When an object is instantiated, the value of each instance variable is determined; Instance variables are created when the object is created and destroyed when the object is destroyed; The value of an instance variable should be referenced by at least one method, constructor or statement block, so that the external can obtain the instance variable information through these methods; Instance variables can be declared before or after use; Access modifiers can modify instance variables; Instance variables are visible to methods, constructors, or statement blocks in a class. 1 Instance variables should be made private in general. Instance variables can be made visible to subclasses by using access modifiers; Instance variables have default values. The default value for numeric variables is 0, and the default value for Boolean variables is 0 false The default value of the reference type variable is null . The value of a variable can be specified at declaration time or in the constructor; Instance variables can be accessed directly by variable name. But in static methods and other classes, you should use fully qualified names: ObejectReference.VariableName。

3. Class variables

Class variables are also declared in the class, outside the method body, but must be declared as static type.

Class variables, also known as static variables, are used in classes with static Keyword, but must be outside the method. No matter how many objects a class creates, the class only has one copy of the class variables. Static variables are rarely used except when declared as constants. Constants refer to those declared as public / private , final And static A variable of type. Constants cannot be changed after initialization. Static variables are stored in static storage. It is often declared as a constant and is rarely used alone static Declare a variable. Static variables are created the first time they are accessed and destroyed at the end of the program. Has similar visibility to instance variables. However, in order to be visible to consumers of classes, most static variables are declared as public Type. The default value is similar to the instance variable. The default value of numeric variables is 0, and the default value of Boolean variables is 0 false The default value of the reference type is null . The value of a variable can be specified at the time of declaration or in the constructor. In addition, static variables can be initialized in static statement blocks. Static variables are available through: ClassName.VariableName The way to access. Class variables are declared 为 public static final Type, the class variable name 1 generally recommends using uppercase letters. If the static variable is not public And final Type, which is named in the same way as instance variables and local variables.

Description of class variables:

The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class. The static can be: Variable (also known as a class variable) Method (also known as a class method) Block.

(Translation)

The static keyword in Java is mainly used for memory management (that is, using this will arrange memory space for sharing a certain amount among instances of the class). We can apply the static keyword of java to variables, methods, statement blocks, and nested classes. static defines objects that belong to the entire class, not instances of a class.

Ordinary variables belong to a specific instance of a class, but variables decorated with the static keyword will belong to a class. That is to say, if we modify a variable value like 1 through a certain instance of the class, only the value in this instance will be modified, and other instances will not be affected; If you modify a class variable modified by the static keyword, the value of all instances of the class will be modified.

It's troublesome to say, you can look at 1 example code:


public class Static {
 public static void main(String[] args) {
  Example foo = new Example();
  Example bar = new Example();

  foo.staticVar = foo.normalVar = "foobar";

  System.out.println(foo.staticVar + "\t" + foo.normalVar);
  System.out.println(bar.staticVar + "\t" + bar.normalVar);
 }
}

class Example {
 static String staticVar = "Example";
 String normalVar = "Example";
}

Output:


foobar foobar
foobar Example

As you can see, we have only changed foo Instance, but the value of the static Modified class variable staticVar The change is in bar While ordinary member variables are not.

Using this feature, we can realize a class that can count the number of its own instances.

For example:


public class CountingClass {
 public static void main(String[] args) {
  Counter a = new Counter();
  Counter b = new Counter();
  Counter c = new Counter();

  System.out.println("Total Counters: " + a.total + " == " + b.total + " == " + c.total);
 }
}

class Counter { //  Self-counting class 
 static int total = 0;

 public Counter() {
  total++;
  System.out.println(" No. 1  " + total + "  A  Counter  Be constructed. ");
 }
}

Related articles: