public class Base { public static final String FOO = "foo"; public static void main(String[] args) { Sub s = new Sub(); System.out.print(((Base)s).FOO); }}
class Sub extends Base {public static final String FOO="bar";}
Can anybody explain why i am getting "Foo". I was thinking "bar" since s is referring to Sub.
I would have said that the reason you got foo instead of bar is that you cast the reference variable to be of type (Base). It seems to me that the compiler determines which variable to use based on the reference type. Try changing the object instantiation in Marc's code to read:
Somebody please correct me if I'm wrong (especially the part about the compiler determining which variable to use).
It's weird that we cook bacon and bake cookies. Eat this tiny ad: