java static final泛型类对象
class TestClass<T> { public T t; public void setT(T t) { this.t = t; } public T getT() { return t; } } public class StaticObj { public static final TestClass<Integer> tc = new TestClass<Integer>(); public static void main(String[] args) { tc.setT(new Integer(101)); System.out.println("tc.t: "+tc.getT()+"\n"); tc.setT(new Integer(102)); System.out.println("tc.t: "+tc.getT()+"\n"); tc.setT(new Integer(103)); System.out.println("tc.t: "+tc.getT()+"\n"); //tc = new TestClass<Integer>(); //cannot assign new value to a final object } }
运行结果:
tc.t: 101
tc.t: 102
tc.t: 103