Java Software Engineer Question:

Download Job Interview Questions and Answers PDF

Tell me what are static initializers and when would you use them?

Java Software Engineer Interview Question
Java Software Engineer Interview Question

Answer:

A static initializer gives you the opportunity to run code during the initial loading of a class and it guarantees that this code will only run once and will finish running before your class can be accessed in any way.

They are useful for performing initialization of complex static objects or to register a type with a static registry, as JDBC drivers do.

Suppose you want to create a static, immutable Map containing some feature flags. Java doesn’t have a good one-liner for initializing maps, so you can use static initializers instead:

public static final Map<String, Boolean> FEATURE_FLAGS;
static {
Map<String, Boolean> flags = new HashMap<>();
flags.put("frustrate-users", false);
flags.put("reticulate-splines", true);
flags.put(...);
FEATURE_FLAGS = Collections.unmodifiableMap(flags);
}
Within the same class, you can repeat this pattern of declaring a static field and immediately initializing it, since multiple static initializers are allowed.

Download Java Software Engineer Interview Questions And Answers PDF

Previous QuestionNext Question
How to override a private or static method in Java?Explain me how can you catch an exception thrown by another thread in Java?