Witscale Test Center

3.6 Code blocks and modifiers > 3.6.2 Static initializer block and its use


3.6.2 Static initializer block and its use

Sometimes it is necessary to do some initialization operation even before a single instance of that class is created. For example, in a class with native method declaration, a native library must be loaded before any of the native methods are called. The best way to load the library before any instance is created is to load it in a static initializer. A static initializer may be replaced by a static method, but a static method needs to be explicitly called to ensure the initialization. The static initializer blocks serve better as they are automatically executed when the class is loaded the first time.