I found that in Java, there is a feature called static block, which includes code that is executed when a class is first loaded (I don’t understand what ‘loaded’ means, does it mean initialized?). Is there any reason to do the initialization bit inside a static block and not in the constructor? I mean, even the constructor does the same thing, do all the necessary stuff when a class is first initialized. is there anything that the static block accomplishes which a constructor can’t?
I found that in Java, there is a feature called static block , which
Share
I first want to highlight one thing thing from your question:
This is incorrect. A constructor does all the initialization necessary when an instance of a class is created. No constructors execute when the class itself is first loaded into memory and initialized (unless an instance of the class happens to be created as part of the class initialization). This confusion (between initializing a class and initializing instances of the class) is probably why you are questioning the utility of
staticblocks.If a class has static members that require complex initialization, a
staticblock is the tool to use. Suppose you need a static map of some kind (the purpose is irrelevant here). You can declare it in-line like this:However, if you want to populate it once, you can’t do that with an in-line declaration. For that, you need a
staticblock:If you wanted to be even more protective, you can do this:
Note that you cannot initialize
initialsin-line as an unmodifiable map because then you couldn’t populate it! You also cannot do this in a constructor because simply calling one of the modifying methods (put, etc.) will generate an exception.To be fair, this is not a complete answer to your question. The
staticblock could still be eliminated by using a private static function:Note, though, that this is not replacing a
staticblock with code in a constructor as you proposed! Also, this won’t work if you need to initialize severalstaticfields in an interrelated way.A case where a
staticblock would be awkward to replace would be a "coordinator" class that needs to initialize several other classes exactly once, especially awkward if it involves dependency injection.Particularly if you don’t want to hard-wire any dependence into
WorkerClass2onWorkerClass1, some sort of coordinator code like this is needed. This kind of stuff most definitely does not belong in a constructor.Note that there is also something called an instance initializer block. It is an anonymous block of code that is run when each instance is created. (The syntax is just like a
staticblock, but without thestatickeyword.) It is particularly useful for anonymous classes, because they cannot have named constructors. Here’s a real-world example. Since (unfathomably)GZIPOutputStreamdoes not have a constructor or any api call with which you can specify a compression level, and the default compression level is none, you need to subclassGZIPOutputStreamto get any compression. You can always write an explicit subclass, but it can be more convenient to write an anonymous class: