“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
A variable type in Java
A local variable
Variables defined in a method or statement block are called local variables. Variable declarations and initializations are in the method, and when the method ends, the variable is destroyed automatically.
- Local variables are declared in a method, constructor, or statement block;
- Local variables are created when a method, constructor, or statement block is executed. When they are executed, the variable is 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.
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, constructors, and class-specific statement blocks in a class.
- Instance variables are declared in a 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 outsiders can retrieve instance variable information in these ways.
- 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. In general, you should make instance variables private. 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
, the default value of a Boolean variable isfalse
, the default value of the reference type variable isnull
. The value of a variable can be specified at declaration time or in the constructor; - Instance variables can be accessed directly by variable names. But in static methods and other classes, fully qualified names should be used:
ObejectReference.VariableName
.
Class variables
Class variables are also declared inside the class, outside the method body, but must be declared static.
- Class variables, also known as static variables, are used in a class
static
The keyword is declared, but must be outside the method. - No matter how many objects a class creates, a class owns only one copy of the class variable.
- Static variables are rarely used except when they are declared as constants. Constants are declared as
public
/private
.final
和static
Type variable. Constants cannot be changed after initialization. - Static variables are stored in static storage. Often declared as constants, rarely used alone
static
Declare variables. - Static variables are created the first time they are accessed and destroyed at the end of the program.
- Similar visibility to instance variables. But for visibility to users of the class, most static variables are declared as
public
Type. - Default values are similar to instance variables. The default value for numeric variables is
0
, the Boolean default value isfalse
, the reference type default value isnull
. The value of a variable can be specified at declaration time or in the constructor. In addition, static variables can be initialized in a static statement block. - Static variables can be passed through:
ClassName.VariableName
To access. - The class variable is declared as
public static final
You are advised to use uppercase letters for class variable names. If the static variable is notpublic
和final
Type, named the same way instance variables and local variables are named.
aboutClass variablesIntroduction:
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 primarily used for memory management (which allocates memory space to be shared between instances of a class). We can apply the Java static keyword to variables, methods, statement blocks, and nested classes. Static defines objects that belong to an entire class, not an instance of a class.
Ordinary variables belong to a particular instance of a class, but variables decorated with the static keyword belong to a class. That is, if we modify a general variable value through an instance of a class, only the value in that instance will be modified, and the other instances will not be affected. If you modify a class variable modified by the static keyword, the value is changed for all instances of the class.
It is more troublesome to say, you can look at a sample 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";
}
Copy the code
Output:
foobar foobar
foobar Example
Copy the code
As you can see, we only changed the value of the foo instance, but the static variable staticVar also changes in bar, while the normal member variable does not.
Using this feature, we can implement a class that counts 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(a) {
total++;
System.out.println("The first" + total + "The Counter is constructed."); }}Copy the code