Java: what is static {}?

Can someone explain to me what it is?

public class Stuff { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch ( ClassNotFoundException exception ) { log.error( "ClassNotFoundException " + exception.getMessage( ) ); } ... } 

What does this static {...} do?

I know about static variables from C ++, but is it a static block or something else?

When will this material be done?

+6
java static-initializer
source share
6 answers

The static block is called class static initializer - it starts the first time the class starts (and this is the only time it is run [footnote]).

The purpose of this particular block is to check if the MySQL driver is in the classpath (and throw / log error if it is not).


[footnote] A static block is run once for the class loader, which loads the class (therefore, if you have several class loaders that are different from each other (for example, do not delegate, for example), it will be executed once each.

+14
source share

The main use of static initializer blocks is to execute various initialization bits, which may not be acceptable inside the constructor, so that together the constructor and initializers put the newly created object in a state that is fully compatible for use.

Unlike constructors, for example, static initializers are not inherited and are executed only once when the class is loaded and initialized by the JRE. In the above example, the class variable foo will have the value 998877 after initialization is complete.

Please also note that static initializers are executed in the order in which they are displayed in text in the source file. In addition, there are a number of restrictions on what you cannot do inside one of these blocks, such as using excluded exceptions, not using the return statement, or this and super keywords.

+8
source share

I want to add that static variables and static initializers are executed in the order of appearance during class loading. So, if your static initializer relies on some kind of static variable, it should be initialized before a specific static block, for example.

 final static String JDBC_DRIVER = getJdbcDriver( ); static { try { Class.forName(JDBC_DRIVER); } catch ( ClassNotFoundException exception ) { log.error( "ClassNotFoundException " + exception.getMessage( ) ); } } 

In this example, getJdbcDriver will execute before the static initializer. In addition, the class may have a more static initializer. Once again they are executed in order of appearance.

I also want to mention the existence of instance initializers here, since they really surprise when they were first seen. They look like a block of code inside the class body, for example.

 class MyClass { final int intVar; { intVar = 1; } } 

In general, their use is somewhat unnecessary due to the constructor, but they are useful in implementing Java versions of closures.

+1
source share

The static initializer block is executed when the class is loaded for the first time. This can happen if something at a higher level does Class#forName("yourpackage.YourClass") or new YourClass() in the class in question for the first time.

Coincidentally, decent JDBC drivers also have something similar inside. They are registered in the DriverManager using a static initializer block:

 static { DriverManager.registerDriver(new ThisDriver()); } 

so whenever you do Class.forName("somepackage.ThisDriver") , you will effectively register the driver with the DriverManager so that you can get a connection from it later.

+1
source share

In addition to everything above, there is a slight difference in the use of the class constructor and the class initializer. The constructor, as we know, will usually be used to initialize objects, and if we have static variables, then a static block is usually used to initialize them when the class is loaded.

When we have static variables and a static block, then the static variables are initialized first, and then the block.

When the class is loaded first, the static block is initialized before the class constructor.

+1
source share

static initialization block

  • is a normal block of code

  • enclosed in braces {}

  • it is preceded by a static keyword

     class Foo { static { // initialization code goes here: doSomething(); } } 
  • a class can have any number of static initialization blocks

  • they can appear anywhere in the class body

  • they are called in apperence order in code

There is an alternative to static initialization blocks:

  • write a private static method
  • and assign it to a variable of a static class

The advantage of this approach is that the static method can be called later to reinitialize the class variable.

 class Foo { public static int myVar = initializeClassVariable(); private static int initializeClassVariable() { // initialization code goes here: int v = 255; return v; } } 
0
source share

All Articles