Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the order of execution is performed between static variables and blocks?

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.

  1. why compilation error is not thrown for the initialization (b=5) as shown in Code:2.

  2. And also please explain why error is been thrown for Code:1, if Code:2 is true?

like image 602
Vasanth Avatar asked Oct 02 '22 05:10

Vasanth


People also ask

What execute first static block or static variable?

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.

Which is executed first static block or instance block?

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.

What should be the execution order if a class has a method static block?

The execution order of the program is that the static block executes first, then instance block, and then constructor.

What is executed first main method or static block?

Important point to note is that static block is executed before the main method at the time of class loading.


1 Answers

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.

like image 194
Ian Roberts Avatar answered Oct 13 '22 12:10

Ian Roberts