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 first want to highlight one thing thing from your question:
the constructor does the same thing, do all the necessary stuff when a class is first initialized
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 static
blocks.
If a class has static members that require complex initialization, a static
block 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:
public static final Map<String, String> initials = new HashMap<String, String>();
However, if you want to populate it once, you can't do that with an in-line declaration. For that, you need a static
block:
public static final Map<String, String> initials = new HashMap<String, String>();
static {
initials.put("AEN", "Alfred E. Newman");
// etc.
}
If you wanted to be even more protective, you can do this:
public static final Map<String, String> initials;
static {
Map<String, String> map = new HashMap<String, String>()
map.put("AEN", "Alfred E. Newman");
// etc.
initials = Collections.unmodifiableMap(map);
}
Note that you cannot initialize initials
in-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 static
block could still be eliminated by using a private static function:
public static final Map<String, String> initials = makeInitials();
private static Map<String, String> makeInitials() {
Map<String, String> map = new HashMap<String, String>()
map.put("AEN", "Alfred E. Newman");
// etc.
return Collections.unmodifiableMap(map);
}
Note, though, that this is not replacing a static
block with code in a constructor as you proposed! Also, this won't work if you need to initialize several static
fields in an interrelated way.
A case where a static
block 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.
public class Coordinator {
static {
WorkerClass1.init();
WorkerClass2.init(WorkerClass1.someInitializedValue);
// etc.
}
}
Particularly if you don't want to hard-wire any dependence into WorkerClass2
on WorkerClass1
, 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 static
block, but without the static
keyword.) It is particularly useful for anonymous classes, because they cannot have named constructors. Here's a real-world example. Since (unfathomably) GZIPOutputStream
does 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 subclass GZIPOutputStream
to get any compression. You can always write an explicit subclass, but it can be more convenient to write an anonymous class:
OutputStream os = . . .;
OutputStream gzos = new GZIPOutputStream(os) {
{
// def is an inherited, protected field that does the actual compression
def = new Deflator(9, true); // maximum compression, no ZLIB header
}
};
java.lang.Math
only contains static methods and is never instantiated for example. (Although Math
contains a constructor, but curiously that constructor is there to make sure no instances are created.) –
Alane static { System.loadLibrary ("foo"); }
–
Alane static
and final
(so that it cannot be changed), the language will not allow you to do it in the constructor. –
Kindergarten static final
field from a constructor. And that's a hard limit, your code will simply not compile. But you can easily have classes that have a single empty private
constructor to prevent instantiation. Even if you put some code in it, it will never be called. (As it's private
, no-one can see it.) –
Alane final
implies that, btw, so forgive me my dumbness)? –
Asyndeton final
variable must be "definitely assigned" a value exactly once. If there is no assignment, or (as far as the compiler can tell) a chance that the assignment will not be executed or will be executed more than once, then it will generate an error. The method Collections.unmodifiableMap
has nothing to do with final
; it takes any Map
and turns it into another Map
that will generate an exception at run time for any attempt to add, modify, or delete any entry in the map. When using both final
and unmodifiable, a static
block is indispensable. –
Kindergarten Constructor is invoked while creating an instance of the class.
Static block is invoked when a classloader loads this class definition, so that we can initialize static members of this class. We should not be initializing static members from constructor as they are part of class definition not object
static
field in a constructor. This technique is used, for instance, to generate a unique id for each instance of a class: define, say, a private static int nextID
field, initialized (statically) to 0, and then in the constructor assign this.id = nextID++;
. –
Kindergarten Static initializer will run if we initialize a class, this does not require that we instantiate a class. But the constructor is run only when we make an instance of the class.
For example:
class MyClass
{
static
{
System.out.println("I am static initializer");
}
MyClass()
{
System.out.println("I am constructor");
}
static void staticMethod()
{
System.out.println("I am static method");
}
}
If we run:
MyClass.staticMethod();
Output:
I am static initializer
I am static method
We never created an instance so the constructor is not called, but static initializer is called.
If we make an instance of a class, both static initilizer and the constructor run. No surprises.
MyClass x = new MyClass();
Output:
I am static initializer
I am constructor
Note that if we run:
MyClass x;
Output: (empty)
Declaring variable x
does not require MyClass
to be initialized, so static initializer does not run.
MyClass x
does not require that the class be loaded or initialized. Java is smart enough to know that it can defer loading and initializing the class until something about it (other than it's name) is actually needed. (By the way, simply referencing a static field of the class will also cause the class to be initialized. You don't need to create an instance or call a method.) –
Kindergarten MyClass
is not created, while constructor is not. This is something that static initializer can do, and constructor cannot do. –
Satiable The static initializer runs when the class is loaded even if you never create any objects of that type.
- Not all classes are meant to be instantiated. The constructor might never be called. It might even be private.
- You may wish to access static fields of the class before you run a constructor.
- The static initializer only runs once when the class is loaded. The constructor is called for each object of that type you instantiate.
You can't initialize static variables with a constructor -- or at least you probably shouldn't, and it won't be particularly useful.
Especially when you're trying to initialize static constants that require significant logic to generate, that really ought to happen in a static block, not a constructor.
They're two separate things. You use a constructor to initialize one instance of a class, the static initialization block initializes static members at the time that the class is loaded.
The static block is reqly useful when you do have to do some action even if no instances is still created. As example, for initializing a static variable with non static value.
static block does different thing than constructor . Basically there sre two different concepts.
static block initializes when class load into memory , it means when JVM read u'r byte code. Initialization can ne anything , it can be variable initialization or any thing else which should be shared by all objects of that class
whereas constructor initializes variable for that object only .
The static block is useful when you want to initialize static fields.
The static block is useful over constructors when you do have to do some action even if no instances is still created. As example, for initializing a static variable with non static value.
One way you can understand static block is; It acts as a constructor. however, the difference between the two is static block instantiates class or static variables while constructor is used to instantiate object variables
Consider the following class
public class Part{
String name;
static String producer;
public Part(String name){
this.name = name;
}
static {
producer = "Boeing";
}
}
objects created from this class will have producer set to Boeing but their name is different depending on the argument passed. for instance
Part engine = new Part("JetEngine");
Part Wheel = new Part("JetWheel");
Initializing static fields in the constructor is a big mistake. Yes, you can initialize static fields in the constructor. However, the static fields will reset their value every time an object is created.
public class StaticExample {
static int myStaticField;
public StaticExample(){
myStaticField = 10;
}
}
public class Solution {
public static void main(String[] args) {
StaticExample obj1 = new StaticExample();
StaticExample.myStaticField+= 80;
// this will print 90
System.out.println(StaticExample.myStaticField);
// creating new object will reset the static field
StaticExample obj2 = new StaticExample();
// this will print 10
System.out.println(StaticExample.myStaticField);
}
}
This is unexpected and unaccepted behavior for a static field. If you now change the initialization step from a constructor to a static block, you will get the correct values.
public class StaticExample {
static int myStaticField;
static {
myStaticField = 10;
}
}
public class Solution {
public static void main(String[] args) {
StaticExample obj1 = new StaticExample();
StaticExample.myStaticField+= 80;
// this will print 90
System.out.println(StaticExample.myStaticField);
// creating new object will NOT reset the static field
StaticExample obj2 = new StaticExample();
// this will print 90
System.out.println(StaticExample.myStaticField);
}
}
If you'd like to get a full understanding of the technical concept (in this scenario - the usage of static blocks in Java), you'd probably need to find the reference to this concept in a real application. Let me attach here a link to the repo of Gomoku game implementation where static block is used in "Board.java" class to initialize a static field "Location".
public class Board {
public static final Iterable<Location> LOCATIONS;
// set up LOCATIONS
static {
ArrayList<Location> l = new ArrayList<Location>(NUM_ROWS * NUM_COLS);
for (int r = 0; r < NUM_ROWS; r++)
for (int c = 0; c < NUM_COLS; c++)
l.add(new Location(r,c));
LOCATIONS = l;
}
}
© 2022 - 2025 — McMap. All rights reserved.
main
and a constructor, which one is invoked earlier when an instance of the class is created? – AsyndetonMain
is invoked, the static block is executed all right. But since theMain
class is itself again invoked inside themain
method, they will create new instances ofMain
. While being instantiated, the static block won't execute, but theirmain
methods will execute, creating two further instances, and so on. – Asyndetonstatic
blocks and static initializers) before any instances are created. (This might be triggered by an attempt to create an instance, but it's a separate process from instance creation.) When you later create instances of that class, the static blocks are not executed again (the same goes for static initializers). Thus there will be no recursion for a class with astatic
block and amain
method. – Kindergartenmain
method is not run every time an instance of a class is created. It is only run when either explicitly invoked (just like any other static method) or when the JVM begins the application. – Dorrstatic
entry point that will be executed implicity by the JVM once. While you certainly can invoke themain
method explicitly and even in a recursively fashion, it definitively sounds like a bad idea :). – Smirch