I have a class, Super
:
public class Super {
public static String foo = "foo";
}
I also have another class, Sub
that extends Super
:
public class Sub extends Super {
static {
foo = "bar";
}
public static void main (String[] args) {
System.out.println(Super.foo);
}
}
When I run it, it prints out bar
.
My third (and last) class is Testing
:
public class Testing {
public static void main (String[] args) {
System.out.println(Super.foo);
System.out.println(Sub.foo);
System.out.println(Super.foo);
}
}
This prints:
foo
foo
foo
I don't understand why the contents of foo
vary depending on what class you're accessing it from. Can anyone explain?