In depth understanding of static keyword in Java

  • 2021-09-04 23:59:00
  • OfStack

For a long time since learning Java, I can't understand why the keywords that modify a method are different, why some methods can be called directly, and some methods need objects to be called. It is no exaggeration to say that the last time I decided to sort out the static keywords, it was because when I wrote the jdbc tool class, I realized the load driver through static code blocks that made me have a new understanding of it.

Generally speaking, it is classified into several points:

A method or member variable decorated by the static keyword can be accessed directly without relying on an object. (Called directly by class name. Method name () or class name. Property) The static keyword does not affect the scope of a variable or method The static keyword does not allow local variables to be decorated You cannot use this in static methods Method or variable: Static methods modified by static cannot access non-static methods or member variables, but conversely, non-static methods can access static or non-static. The static keyword avoids the tedious and resource-intensive process of requiring new objects first, and is often used in tool classes.

Let's introduce the functions of the following static keywords in detail, mainly in the following four aspects:

Modify member variables

Member variables modified by the static keyword are called static member variables, also known as static variables. With static variables, it corresponds to non-static variables

Static variables: Share by all objects, have only 1 copy in memory, and are initialized once if and only when the class is first loaded Non-static variables: Object-owned, initialized when the object is created, and multiple copies exist in memory without affecting each other

public class Test{
	public static void main(String[] args) {
		System.out.println(Person.name);
		System.out.println(new Person().age);
	}	
}
class Person{
	static String name; 
	int age;
}

Modification method

static-modified methods are called static methods. Static methods can directly call static variables and other static methods of a class, but cannot directly call member variables and non-static methods (except through objects).


class A {
	static {
		System.out.println(" Static code block ");
	}
	static final int i;
	int j;
	static void method() {
		System.out.println(i);// Direct access to static variables 
		System.out.println(new D().j);// Accessing non-static variables in static methods requires calling through objects 
		
		method1();// Call static methods directly 
		new D().method2();// Accessing non-static methods in static methods requires calling through objects 
	}
	
	static void method1() {
		System.out.println(i);// Direct access to static variables in static methods 
	}
	void method2() {
		System.out.println(i);// Static variables can be accessed directly in non-static methods 
	}
}

Static code block

The code block modified by static is called a static code block. When the class is first loaded, it is executed once in the order of code blocks. Used to optimize the program.
For example, in the JDBC tool class mentioned at the beginning, static code blocks are used to load resources, which are executed only once when the class is initialized.
Note: Constructors are used for object initialization, and static code blocks are used for class initialization.

Static guide pack

Compared with the above three kinds, the fourth kind is something I haven't touched before.
When importing a package or class with import, you can modify the package name or class with static to indicate a static import. Different from non-static import, after adopting static import package, there is no need to use the method of "class name. Method name" to call class methods without conflicting with the method name of the current class, and you can directly use "method name" to call class methods, just like the method 1 of the class itself.


public class Test {
	static void method1() {
		System.out.println("static method1");
	}
	
	static void method2() {
		System.out.println("static method2");
	}
}

After static import:


import static com.liu.stuatic.Test.method1;

public class Client {
	public static void main(String[] args) {
		method1(); //  Methods imported statically are called directly without the class name 
		StaticTest.method2();
	}
}

Related articles: