Saturday, June 21, 2008

static field shared between instances of generic

The documentation says that one implementation is shared. I guess it makes sense this translates to one copy of the class variables getting shared as well.
public class GenericStatic<T> {
static String o = "";
String oo() {
return o += "o";
}

public static void main(String[] args) {
GenericStatic<Object> a = new GenericStatic<Object>();
GenericStatic<String> b = new GenericStatic<String>();
System.out.println(a.oo());
System.out.println(b.oo());
}
}
$ javac GenericStatic.java
$ java GenericStatic
o
oo

No comments: