Насколько я понял, "блок статической инициализации" используется для установки значений статического поля, если это невозможно сделать в одной строке.
Но я не понимаю, зачем нам для этого нужен специальный блок. Например, мы объявляем поле как статическое (без присвоения значения). А затем напишите несколько строк кода, которые генерируют и присваивают значение вышеуказанному статическому полю.
Зачем нам нужны эти строки в специальном блоке типа: static {...}?
Переведено автоматически
Ответ 1
Нестатический блок:
{ // Do Something... }
Вызывается каждый раз, когда создается экземпляр класса. Статический блок вызывается только один раз, когда инициализируется сам класс, независимо от того, сколько объектов этого типа вы создаете.
Если бы их не было в блоке статической инициализации, где бы они были? Как бы вы объявили переменную, которая должна быть локальной только для целей инициализации, и отличили ее от поля? Например, как бы вы хотели написать:
publicclassFoo { privatestaticfinalint widgets;
static { intfirst= Widgets.getFirstCount(); intsecond= Widgets.getSecondCount(); // Imagine more complex logic here which really used first/second widgets = first + second; } }
Если бы first и second не были в блоке, они выглядели бы как поля. Если бы они были в блоке без static перед ним, это считалось бы блоком инициализации экземпляра, а не блоком статической инициализации, поэтому он выполнялся бы один раз на созданный экземпляр, а не один раз в целом.
Now in this particular case, you could use a static method instead:
staticintgetWidgets() { intfirst= Widgets.getFirstCount(); intsecond= Widgets.getSecondCount(); // Imagine more complex logic here which really used first/second return first + second; } }
... but that doesn't work when there are multiple variables you wish to assign within the same block, or none (e.g. if you just want to log something - or maybe initialize a native library).
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.
Ответ 4
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.
Hey, there's another benefit, you can use it to handle exceptions. Imagine that getStuff() here throws an Exception which really belongs in a catch block:
To come back to the JDBC driver example, any decent JDBC driver itself also makes use of the static initializer to register itself in the DriverManager. Also see this and this answer.