I understand that the following is an example of valid reference in java. I also understand that at class creation, first i and j are initialized to 0. j is then re-initialized to 5, as a result of which i's value is 0 and j's is 5 inside main.
class J { static int test() { return j; } static int i = test(); static int j=5; public static void main(String[] args) { System.out.println(i); System.out.println(j); } }
But why is the same not true for the following ? Why is it that i and j are not initialized to the default value of 0 in this case and gives an illegal forward reference?
class test { static int i = j; static int j=5; public static void main(String[] args) { System.out.println(i); System.out.println(j); } }
Why is it legal forward reference when using a method, and illegal when pretty much the same thing is done using a variable ?
Please advise.
Thanks.