2
class StaticControlFlow {

    static int x = 10;
    static int y ;


    //First Static Block 
    static {

        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(a);
        m1();
        System.out.println("static block 1");
    }

    // Main Method
    public static void main(String[] args){

        System.out.println("main method");
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(a);
        m1();
    }

    // Static method
    public static void m1(){
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(a);

        System.out.println("static method m1");

    }

    // Second Static Block 
    static {

        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(a);
        m1();
        System.out.println("Second Static Block ");
    }


    static int z = 100;
    static int a ;

}

Error :-

static_control_flow>javac StaticControlFlow.java

StaticControlFlow.java:12: error: illegal forward reference
                System.out.println(z);
                                   ^
StaticControlFlow.java:13: error: illegal forward reference
                System.out.println(a);
                                   ^
StaticControlFlow.java:45: error: illegal forward reference
                System.out.println(z);
                                   ^
StaticControlFlow.java:46: error: illegal forward reference
                System.out.println(a);
rgettman
  • 176,041
  • 30
  • 275
  • 357
Rahul
  • 21
  • 2
  • 1
    Forward reference errors only apply in [specific circumstances](https://stackoverflow.com/a/29153929/1553851). – shmosel Feb 26 '19 at 20:20

2 Answers2

1

Basically, static variables and blocks are evaluated first, then the compiler does a second run and evaluates the static methods, then it goes to all instance variables, blocks and methods.

This is why it is possible to call all static variables from the m1() method

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

Static blocks are executed as they're defined, and before static fields are initialized.
As you can see you're using

static int z = 100;
static int a;

inside a static block, before they're correctly initialized.


Anyway, accessing those static fields calling m1 inside a static block will simply print 0, as they're not property initialized and default values are used.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • I think OP wants to know why the same static block can reference `z` indirectly, via `m1()`. – shmosel Feb 26 '19 at 20:18
  • Yes , this is my doubt how this works indirect read of static variable not resulting in forward reference error. – Rahul Feb 27 '19 at 10:08
  • @Rahul because the compiler assumes that when the `m1` gets called, all the fields are properly initialized. That's by Java Language Specification – LppEdd Feb 27 '19 at 10:15