As far as I understood the "static initialization block" is used to set values of static field if it cannot be done in one line.
But I do not understand why we need a special block for that. For example we declare a field as static (without a value assignment). And then write several lines of the code which generate and assign a value to the above declared static field.
Why do we need this lines in a special block like: static {...}
?
It's also useful when you actually don't want to assign the value to anything, such as loading some class only once during runtime.
E.g.
Hey, there's another benefit, you can use it to handle exceptions. Imagine that
getStuff()
here throws anException
which really belongs in a catch block:then a
static
initializer is useful here. You can handle the exception there.Another example is to do stuff afterwards which can't be done during assigning:
To come back to the JDBC driver example, any decent JDBC driver itself also makes use of the
static
initializer to register itself in theDriverManager
. Also see this and this answer.There are a few actual reasons that it is required to exist:
static final
members whose initialization might throw an exceptionstatic final
members with calculated valuesPeople tend to use
static {}
blocks as a convenient way to initialize things that the class depends on within the runtime as well - such as ensuring that particular class is loaded (e.g., JDBC drivers). That can be done in other ways; however, the two things that I mention above can only be done with a construct like thestatic {}
block.So you have a static field (it's also called "class variable" because it belongs to the class rather than to an instance of the class; in other words it's associated with the class rather than with any object) and you want to initialize it. So if you do NOT want to create an instance of this class and you want to manipulate this static field, you can do it in three ways:
1- Just initialize it when you declare the variable:
2- Have a static initializing block:
3- Have a class method (static method) that accesses the class variable and initializes it: this is the alternative to the above static block; you can write a private static method:
Now why would you use static initializing block instead of static methods?
It's really up to what you need in your program. But you have to know that static initializing block is called once and the only advantage of the class method is that they can be reused later if you need to reinitialize the class variable.
let's say you have a complex array in your program. You initialize it (using for loop for example) and then the values in this array will change throughout the program but then at some point you want to reinitialize it (go back to the initial value). In this case you can call the private static method. In case you do not need in your program to reinitialize the values, you can just use the static block and no need for a static method since you're not gonna use it later in the program.
Note: the static blocks are called in the order they appear in the code.
Example 1:
Example 2:
As supplementary, like @Pointy said
It's supposed to add
System.loadLibrary("I_am_native_library")
into static block.It will guarantee no native method be called before the related library is loaded into memory.
According to loadLibrary from oracle:
So quite unexpectedly, putting System.loadLibrary is not used to avoid library be loaded multi-times.
Here's an example:
The code in the "static" section(s) will be executed at class load time, before any instances of the class are constructed (and before any static methods are called from elsewhere). That way you can make sure that the class resources are all ready to use.
It's also possible to have non-static initializer blocks. Those act like extensions to the set of constructor methods defined for the class. They look just like static initializer blocks, except the keyword "static" is left off.
You first need to understand that your application classes themselves are instantiated to
java.class.Class
objects during runtime. This is when your static blocks are ran. So you can actually do this:and it would print "myInt is 1" to console. Note that I haven't instantiated any class.