How strange these two examples are!!!
Would anybody like to explain how these two examples happen? and why the second example compiles without saying "illegal forward reference?
----------------------------------------------------------------
Example1:
public class MyClass
{
private static int x = y;
private static int y = 5;
private static int getValue()
{
return y;
}
public static void main(String args[])
{
System.out.println(x);
}
}
Result: compiler error, compiler says illegal forward reference
----------------------------------------------------------------------------
Example2
public class MyClass
{
private static int x =getValue();
private static int y = 5;
private static int getValue()
{
return y;
}
public static void main(String args[])
{
System.out.println(x);
}
}
Result: print "0"
----------------------------------------------------------------------------