java static final泛型类对象

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()+"
");

tc.setT(new Integer(102));

System.out.println("tc.t: "+tc.getT()+"
");

tc.setT(new Integer(103));

System.out.println("tc.t: "+tc.getT()+"
");

//tc = new TestClass<Integer>(); //cannot assign new value to a final object

}

}

运行结果:

tc.t: 101

tc.t: 102

tc.t: 103

  

原文地址:https://www.cnblogs.com/aspirs/p/11446685.html