What is static constructor java?

3.7K    Asked by MatthewTAYLOR in Java , Asked on Oct 11, 2022

 I didn't get a complete understanding of static constructors in Java. If it is allowed, why is it allowed? In what scenarios would you use it? What purpose would it serve? Can you give me a simple example please?


Answered by Jordan Duncan

There is no static constructor java because a constructor, by definition, cannot be static. What you are referring to is called a "static initialization block." A constructor implies that you are constructing an object. You cannot have a constructor for a class because a class is not an instance of itself. It is simply a class.


All nit-picking aside, a static initialization block is used to initialise complex static (or class-level) fields for a class. Usually these are used to initialise things that either cannot be initialised in one line, or require that some other object (which may or may not be in the class in which the static block is implemented) be initialised first.

Basically, one might use them to tell the class "Hey, set variable A to this value FIRST, then, once that's done, use A's value to initialise B." Since Java requires that standard field initialization be done either within a constructor or method, or via the call of a constructor or method (unless it is a literal), these can be a convenient method for initialising complex, static objects.

Static initialization blocks are not needed all too often, and generally should be avoided unless they have a real use. Don't get me wrong, they have their place in Java, but like many other things (such as break, return, switch, and goto statements) they can be easily over-used, which reduces their readability and the maintainability of the code-base they are used in.

A brief example of a static initialization block being used would be the following (as per the excellent explanation of static initialization blocks found here):

Code:

public class StaticExample{
    static {
        System.out.println("This is first static block");
    }
    public StaticExample(){
        System.out.println("This is constructor");
    }
    public static String staticString = "Static Variable";
    static {
        System.out.println("This is second static block and "
                                                    + staticString);
    }
    public static void main(String[] args){
        StaticExample statEx = new StaticExample();
        StaticExample.staticMethod2();
    }
    static {
        staticMethod();
        System.out.println("This is third static block");
    }
    public static void staticMethod() {
        System.out.println("This is static method");
    }
    public static void staticMethod2() {
        System.out.println("This is static method2");
    }
}

Output:

This is first static block

This is second static block and Static Variable

This is static method

This is third static block

This is constructor

This is static method2

Some instances they list when static blocks can be useful:

If you’re loading drivers and other items into the namespace. For example, Class class has a static block where it registers the natives.

If you need to do computation in order to initialise your static variables,you can declare a static block which gets executed exactly once,when the class is first loaded.

Security related issues or logging related tasks

Some reasons NOT to use static blocks (in other situations):

There is a limitation of JVM that a static initializer block should not exceed 64K.

You cannot throw Checked Exceptions.

You cannot use this keyword since there is no instance.

You shouldn’t try to access super since there is no such a thing for static blocks.

You should not return anything from this block.

Static blocks make testing a nightmare.

I should note: While some languages (such as C#) may have syntax for "constructors" that are static, those "constructors" function much the same way that static initialization blocks do in Java, and are seen by many (myself included) as misnomers in the language, given the basic concept of an OOP constructor.



Your Answer

Interviews

Parent Categories