Consider the below scenario:
Code:1
public class StaticDemo {
static{
b=5;
System.out.println("Static B:"+b);/*Compilation error:"Cannot reference a field before it is defined"*/
}
static int b;
static{
System.out.println("B:"+b);
}
public static void main(String[] args) {
}
}
Commenting the code as below, there are no errors and following output has been displayed.
Code:2
public class StaticDemo {
static{
b=5;
//System.out.println("Static B:"+b);
}
static int b;
static{
System.out.println("B:"+b);
}
public static void main(String[] args) {
}
}
Output-
B:5
If the execution is based on the order in which static variables or blocks have been written.
why compilation error is not thrown for the initialization (b=5
)
as shown in Code:2.
And also please explain why error is been thrown for Code:1, if Code:2 is true?
Execution Order: There is an order in which static block/method/variable gets initialized. Static Blocks are called even before the main method which is nothing but a static method i.e. execution point of every class.
Order of execution When you have all the three in one class, the static blocks are executed first, followed by constructors and then the instance methods.
The execution order of the program is that the static block executes first, then instance block, and then constructor.
Important point to note is that static block is executed before the main method at the time of class loading.
From the Java Language Specification §8.3.2.3
The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
- The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
- The usage is not on the left hand side of an assignment.
- The usage is via a simple name.
- C is the innermost class or interface enclosing the usage.
It is a compile-time error if any of the four requirements above are not met.
[...]
The restrictions above are designed to catch, at compile time, circular or otherwise malformed initializations
In other words, it's OK to write to a field that is declared later in the class, but not to read from it.
There are more detailed examples of this in the example box immediately following the section I have quoted.
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