On the function of static keyword in Java

  • 2021-07-13 05:06:50
  • OfStack

The static keyword serves two main purposes:

First, for a specific data type or object allocation of single 1 storage space, regardless of the number of objects created.

Second, implement a method or property associated with a class instead of an object in 1

Specifically, in Java language, static has four main uses: member variables, member methods, code blocks and internal classes

(1) static member variables:

The Java class provides two types of variables: static variables decorated with the static keyword and instance variables not decorated with the static keyword. A static variable belongs to a class and has only one copy in memory. As long as the class in which the static variable belongs is loaded, the static variable will be allocated space, so it can be used. There are two ways to refer to static variables, which are "class. static variables" and "object. static variables"

Instance variable belongs to object, only after the object is created, the instance variable will be allocated memory space and can be used. It has multiple copies in memory and can only be referenced in the way of "object. Instance variable".

(2) static membership method:

static method and non-static method are provided in Java. The static method is a method of a class, which can be called without creating an object, while the non-static method is a method of an object, which can only be used after the object is created

this and super keywords cannot be used in static methods, non-static methods cannot be called, and only static member variables and member methods of the class to which they belong can be accessed, because when static methods are called, the objects of this class may not have been created, and even if they have been created, it is impossible to determine which object's method to call. Similarly, the static method cannot access variables of non-static type.

Singleton design pattern:

One of the most important uses of static1 is to implement singleton design patterns. The characteristic of simple profit mode is that there can only be one instance of this class, To realize this 1 function, You must hide the constructor of the class, That is, the constructor is declared as private, and a method for creating objects is provided. Because the constructed object is declared as private, the outside world cannot directly create this type of object, and the object of the class can only be obtained through the method provided by this class. To achieve this goal, the method for creating objects can only be declared as static. The program example is as follows:


class Singleton{
	private static Singleton instance=null;
	private Singleton(){}
	public static Singleton getInstance(){
		if(instance==null){
			instance=new Singleton();
		}
		return instance;
	}
}

(3) static code block

The static code block is independent of the member variable and member function code block in the class. Note: These static code blocks will only be executed once

(4) The meaning of static in combination with final:

For variables, if static final modification is used, it means that 1-denier assignment cannot be modified and can be accessed by class name.
For a method, the static final modification means that the method is not overridden and can be accessed directly by the class name.


public class Test{
	public static int testStatic(){
		static final int i=0;
		System.out.println(i++);
		
	}
	public static void main(String[] args){
		Test test=new Test();
		test.testStatic();
	}
}

The result of the above program running:

A 0 B 1 C 2 D compilation failed

Answer: D

In the Java language, you cannot define an static variable inside a member function


Related articles: