This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging
We’ve talked about the use of this in Java, and today we’ll continue to talk about the use of this in JavaThe static keyword, which is divided into two updates. The main contents of this article are as follows:
- Static keyword in Java
- Static in Java
The static keyword in Java
Static is a keyword in the Java language that means “static.” It can be used to modify variables, methods, code blocks, etc. Modified variables are called static variables, modified methods are called static methods, and modified code blocks are called static code blocks.
In the Java language, all static modifiers are class-dependent. There is no need to create an object. They are accessed directly by the class name, and even if they are accessed by reference, they run independently of the object in the heap.
Sometimes you want to define a class member so that its use is completely independent of any object in the class. Normally, a class member must be accessed through an object of its class, but it is possible to create a member that can be used by itself without having to refer to a specific instance. Such members are created by prefixing the member declaration with the keyword static. If a member is declared static, it can be accessed before any object of its class is created, without having to refer to any object. You can declare both methods and variables static. The most common example of a static member is main (). Because main() must be called when the program begins execution, it is declared static. \
-
Variables declared static are called static or class variables. You can refer to a static variable directly by its class name or by its instance name, but it’s better to use the former because the latter can confuse static variables with generic variables. A static variable is associated with a class. All instances of a class share a static variable. \
-
Methods that are declared static are called static or class methods. Static methods can directly call static methods and access static variables, but cannot directly access instance variables and instance methods. You cannot use this keyword in static methods because static methods do not belong to any instance.
Static in Java
Java variables include: local variables and member variables, declared in the body of the method variables are local variables, effective range is very small, can only be accessed in the body of the method, local variables after the end of the method memory is released, in memory local variables stored in the stack.
Variables defined in the class body for the member variable, and member variables, including instance variables and static variables, when the member variable declarations using the static keyword, then this is called static variables, not using the static keyword is referred to as an instance variable, instance variables are object level, each object instance variable values may be different, Therefore, instance variables must first create an object and access it through “reference”, whereas static variables do not need to create an object and access it directly through “class name”.
Instance variables are stored in the heap and static variables are stored in the method area. Instance variables are initialized during constructor execution, and static variables are initialized during class loading.
So when are variables declared static?
Look at the following code to define a “man” class:
Public class Man {// id id int idCard; Boolean "sex = true"; Boolean "sex = true"; public Man(int idCard){ this.idCard = idCard; }}Copy the code
public class ManTest { public static void main(String[] args) { Man jack = new Man(100); System.out.println(jack.idCard + "," + (jack.sex ? "Male" : "female ")); Man sun = new Man(101); System.out.println(sun.idCard + "," + (sun.sex ? "Male" : "female ")); Man cok = new Man(102); System.out.println(cok.idCard + "," + (cok.sex ? "Male" : "female ")); }}Copy the code
The running result is as shown in the figure below:
Figure 1: Run results
Let’s take a look at the memory structure of the above program:
Figure 2: Memory structure
“Male human” create all of the “man”, every object of “men” id number is different, this property should be each object holds a, so should be defined as an instance variable, and each object of “men” gender is a “male”, will not change with the change of the object, the gender value always will be a “male”, this kind of circumstance, Gender also need to define the variables for the instance variable, it is necessary for every object of “man” held a, so not to waste a lot of heap memory space, so this time suggest defined gender = “male” attribute as a class level attributes, declared as static variables, to rise to the “entire family” data, Such a variable can be accessed directly using the “class name” without creating an object.
Look at the code:
Public class Man {// id id int idCard; Static Boolean = true; static Boolean = true; public Man(int idCard){ this.idCard = idCard; }}Copy the code
public class ManTest { public static void main(String[] args) { Man jack = new Man(100); System.out.println(jack.idCard + "," + (Man.sex ? "Male" : "female ")); Man sun = new Man(101); System.out.println(sun.idCard + "," + (Man.sex ? "Male" : "female ")); Man cok = new Man(102); System.out.println(cok.idCard + "," + (Man.sex ? "Male" : "female ")); }}Copy the code
The running result is as shown in the figure below:
Figure 3: Run results
Let’s take a look at the memory structure of the above program:
Figure 4: Memory diagram for static variables
We’ve learned through the study of the above content, when all objects of a class of one “attribute value” will not change with the change of the object, proposed to this property is defined as static attributes (or the variables defined as static variables), static variables are initialized when class loading, storage in the method, do not need to create objects, Access is directly by class name. Is it ok if static variables are accessed by “reference”, and if so, is the access related to the specific object?
Look at the following code:
Public class ManTest {public static void main(String[] args) {system.out.println (" gender = "+ man.sex); // Create object Man jack = new Man(100); // Is it ok to use "references" to access static variables? System.out.println(" gender = "+ jack.sex); // The object was reclaimed by the garbage collector. // Is it still accessible using "reference"? System.out.println(" gender = "+ jack.sex); }}Copy the code
The running result is as shown in the figure below:
Figure 5: Static variables are accessed by reference
Can be seen through the above code and operation result, static variables can also be used to visit the “reference”, but actually in the process of execution, the object was not involved in the “reference” points to, if it is null reference to access the instance variables, the program will be null pointer exception occurs, but the above program compile, and run also without any exception, This shows that although it appears to be accessed by “reference”, it is actually accessed directly by “class” at runtime. Is that true for static methods?
Look at the following code:
Public class Man {// id id int idCard; Static Boolean = true; static Boolean = true; public Man(int idCard){ this.idCard = idCard; } public static void printInfo(){system.out.println ("-----" + (man.sex? "Male" : "female ") + "------"); }}Copy the code
Public class ManTest {public static void main(String[] args) {system.out.println (" gender = "+ man.sex); // Create object Man jack = new Man(100); // Is it ok to use "references" to access static variables? System.out.println(" gender = "+ jack.sex); // The object was reclaimed by the garbage collector. // Is it still accessible using "reference"? System.out.println(" gender = "+ jack.sex); Man.printinfo (); man.printinfo (); // Can static methods be accessed using references? And an empty reference is ok? jack.printInfo(); }}Copy the code
The running result is as shown in the figure below:
Figure 6: Can static methods be accessed using references
From the above code tests, we know that static variables and static methods are officially accessed directly by “class name”, but in fact can be accessed by “reference”, and there is no null pointer exception when accessing static variables and static methods with a null reference. In fact, using “references” to access statically related members is not recommended in development because it confuses the programmer, who thinks you are accessing instance-related members.
In general, all instance-related objects, including instance variables and instance methods, must first be created and then accessed by reference. If a null reference accesses an instance-related member, a null pointer exception will occur. All statically related variables, including static variables and static methods, are accessed directly using the “class name”. Statically related members can also be accessed using “references,” but this approach is not advocated.