class TestEntity {
public int x;
public int y;
public TestEntity(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return super.toString() + ", x -> " + x + ", y -> " + y;
}
}
TestEntity t = new TestEntity(666, 777);
List<TestEntity> list = new ArrayList<>();
list.add(t);
t = null;
System.out.println(list.get(0));
为什么正确的打印list.get(0)
与列表后,添加一些对象,并设置对象空
@ XXXXX,X - > 666,Y - > 777
如果我删除t = null;
,做t.x = 888
,打印看起来是正确的。
你能在问题上更清楚一点吗? – Stultuske
't = null;'只是取消引用变量't',但不会改变它之前引用的TestEntity对象上的任何内容。你应该学习/搜索关于“java参考” – Joel
你能定义你的结果,以及你的期望吗? – Nathan