inheriting statics

Dave Herman dherman at ccs.neu.edu
Tue Jan 9 07:52:16 PST 2007


>> IIRC static data members in Java are inherited but cannot be
>> overridden, which may provide a bit of a catch-22 for #1 and #2?

Are we sure? I tried the following in Java 5:

// --
public class C1 { public static int foo = 10; }
public class C2 extends C1 { public static int foo = 1000; }
public class Main {
     public static void main(String[] args) {
         C2 x = new C2();
         System.out.println(x.foo);
         System.out.println(((C1)x).foo);
         System.out.println(((C2)x).foo);
     }
}
// --

and it compiled, ran, and printed:

1000
10
1000

In other words, statics are inherited and can be redefined, although 
they are statically resolved, so they have shadowing semantics (like 
instance fields) rather than overriding.

Dave



More information about the Es4-discuss mailing list