Once a class is loaded is there a way to invoke static initializers again?
public class Foo {
static {
System.out.println("bar");
}
}
Edit:
I need to invoke the static initializer because I didn't write the original class and the logic I need to invoke is implemented in the static initializer.
2. Static Block. Static initializer block or static initialization block, or static clause are some other names for the static block. Static block code executes only once during the class loading.
There can be multiple static initialization blocks in a class that is called in the order they appear in the program.
The static initializer is actually invoked later, when the class is initialized, after it has been loaded and linked. That happens when you instantiate an object of a class or access a static variable or method on the class.
Class static initialization blocks are a special feature of a class that enable more flexible initialization of static properties than can be achieved using per-field initialization.
Put the initalisation code in a separate public static method, so you can call it from the static initializer and from elsewhere?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With