A static block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
Questions tagged [static-block]
166 questions
4
votes
0 answers
JDK11 to JDK12 Migration java.lang.NoSuchFieldException: modifiers
Below function Hacks the "HttpURLConnection#methods" static field (through Java Reflection). I am using reflection to unit test my code functionality. I found out we can not change the static final fields in JDK12. I found one solution to use unsafe…

Mutahir Kiani
- 83
- 1
- 1
- 11
4
votes
5 answers
Static Block and Main Thread
I found a very interesting thing while trying with java. Please find the code below:
public class SimpleTest {
static{
System.out.println(Thread.currentThread().getName());
System.exit(0);
}
}
The above program runs…

Brinal
- 172
- 2
- 18
4
votes
1 answer
Couchbase: Initialization from within static code block takes longer
I put my couchbase initialization code inside a static code block:
static {
initCluster();
bucket = initBucket("graph");
metaBucket = initBucket("meta");
BLACKLIST = new SetObservingCache(() -> getBlackList(),…

KidCrippler
- 1,633
- 2
- 19
- 34
4
votes
3 answers
Can one force execution of the static blocks of all the files in the application?
My goal is to have several classes to have a block of code that runs at the beginning of the program. Assume for a second that classes can be added to the project on a whim, and it will be impractical to have a static function that is called in the…

v010dya
- 5,296
- 7
- 28
- 48
4
votes
0 answers
static block in a class not executed though it is being referenced
I stumbled upon following code
class Super {
static String ID = "QBANK";
}
class Sub extends Super {
static {
System.out.print("In Sub");
}
}
public class Test{
public static void main(String[] args){
…

Aniket Thakur
- 66,731
- 38
- 279
- 289
4
votes
0 answers
Static initializers and final constants in Java
Given the following simple code in Java.
final class Demo
{
public static final long serialVersionUID=1L;
static
{
System.out.println("Static constructor invoked.");
}
}
public final class Main
{
public static void…

Tiny
- 27,221
- 105
- 339
- 599
4
votes
2 answers
Behavior of static blocks with Abstract classes
Child Class:
public class ChildExtending extends ParentAbstract{
public int childInt =111213;
static{
System.out.println("child static block executed");
}
public ChildExtending() {
System.out.println("child const…

ALBI
- 721
- 2
- 16
- 34
4
votes
2 answers
multithreading and static blocks
I am facing a weird problem in one of our projects. We use JUnit to run our unit tests, and some time ago, we started to run pur tests in parallel to speed up execution. Most of the time, all is fine, but from time to time, nearly all of our tests…

Axel
- 13,939
- 5
- 50
- 79
3
votes
2 answers
How to detect time of ClassLoading
I have a TrirdParty API that contains a CLass [let's say A]. It has a bizarre static block similar to the following:
class A
{
static
{
try
{
System.loadLibrary("libraryName");
}
…

Swaranga Sarma
- 13,055
- 19
- 60
- 93
3
votes
3 answers
why static block cannot access the static variable defined after it
I've checked Forward References During Field Initialization and this the answer from @assylias, but still I got no answer to the why.
Why a static block can assign the static variable declared after it but can NOT access it?
class Parent {
…

Hearen
- 7,420
- 4
- 53
- 63
3
votes
0 answers
Are Sping Beans loaded and initialized before static block of other Java classes gets executed?
In one of our legacy code base, I found the following pattern being used which seems to be a bit fragile. Consider the following Spring Bean:
@Component
public class PropsProvider {
private static Properties props;
@Inject
…

6harat
- 542
- 6
- 18
3
votes
5 answers
Why does my static block allows to call parent class static method without using parentclass reference?
From what I understand, usually the static method should be called using class's reference or it can be called directly without reference if its in a static method or static block.
But does this apply when static method is called from child class…

amarnath harish
- 945
- 7
- 24
3
votes
1 answer
What guarantees that this call to a static method from another class's static block works as expected?
Class A contains:
static Strings
a static Map that is populated in the class's static
block
a static method, say getStr(), that returns a String built from the static
Strings
Class B contains:
a static Map
a static block that populates the Map…

foamroll
- 752
- 7
- 23
3
votes
1 answer
Magento: how to add a static block AFTER product list
In a web site my boss wants me to insert a static block AFTER the product list of that category. Up to now, using the Magento front end application, the one you can see here, I have seen that I can add a static block only before the product list.…

SagittariusA
- 5,289
- 15
- 73
- 127
3
votes
5 answers
why static block in child class does not get executed?
here is the code
public class ClassResolution {
static class Parent {
public static String name;
static {
System.out.println("this is Parent");
name = "Parent";
}
}
static class Child extends Parent {
static {
…

turtledove
- 25,464
- 3
- 23
- 20