Usage of static {}

The static keyword is very interesting. 

public static String getProperty(String key) { Needless to say,

static {}

is called static code block, also called static code block. It is a static code

in a class that is independent of class members. There can be more than one statement block, and the location can be placed at will. It is not in any method body. When the JVM loads the class, these static code blocks will be executed. If there are multiple static code blocks, the JVM will follow the order in which they appear in the class. Execute them in sequence, and each code block will only be executed once

. Using static code blocks, some static variables can be assigned
 
 

First of all, you need to know that static{} (static code block) static is loaded before the constructor in program loading, and will only be loaded once.
Only static-modified properties can be used in two static blocks.
The static{} block in the program is only to load the properties file information, this loading will only be loaded once.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325850156&siteId=291194637