Legal forward reference assignment weirdness on declarations
by , 08-11-2011 at 11:38 PM (1062 Views)
Observer the glorious code by your's truly Hibernate:
Obviously you can not assign a variable to the value of it self before it is declared, hence youJava Code:public class YetAnotherClass { //public static final int YET_ANOTHER_CONSTANT = YET_ANOTHER_CONSTANT; //Compile-time error: illegal forward reference public static final int ANOTHER_CONSTANT = YetAnotherClass.ANOTHER_CONSTANT; //OK! //public int yetAnotherVariable = yetAnotherVariable; //Compile-time error: illegal forward reference public int anotherVariable = this.anotherVariable + 1; //OK! public static void main(final String... args) { System.out.println(ANOTHER_CONSTANT); System.out.println(new YetAnotherClass().anotherVariable); } }
can not write the two commented lines. But if you refer to them in a fully qualified manner, you can!
It is not a bug, it goes hand in hand with the Java specifications.
If you run this code, it prints:
0
1
When value of it the referred to variable is 0, false or null (the default value), if it has not yet been declared.









Email Blog Entry
sorry for all the questions
thanks...
06-14-2013, 02:22 PM in gbonecapone