为什么这个对我的实例的静态引用不更新内部值?
Why does this static reference to my instance not update internal values?
我有一段相对复杂的代码,它使用 Gson 序列化对象以便为它们保存非常具体的值。除此之外,还有一个特定的对象控制着相当大的代码部分,并且经常访问它,所以为了减少不必要的列表管理来找到它(这些配置对象存储在列表中),我创建了一个静态引用以更快地访问它,但它似乎不适用于该对象。
我已经简化了代码,这里的关系让我感到困惑。如果我使用下面的代码,active
将不会在对 this
:
的静态引用中更新
public static void main(String[] args) {
String json = "{ \"active\": false }";
final MyObject myObject = new MyObject();
final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
myObject.fromGson(gson.fromJson(json, MyObject.class));
for (int i = 0; i < 10; i++) {
myObject.setActive(!myObject.isActive());
}
}
private static class MyObject {
@Expose private boolean active;
private static MyObject self;
public MyObject() {
this.active = false;
self = this; // notice where this line is located
}
public static boolean active() {
return self.isActive();
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
System.out.println("this: " + this.isActive());
System.out.println("self: " + self.isActive());
}
public void fromGson(MyObject object) {
this.setActive(object.isActive());
}
}
测试时,输出:
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
显然,self
没有改变,即使它应该只是访问 this.isActive()
。现在考虑以下更改:
public MyObject() {
this.active = false;
}
public void fromGson(MyObject object) {
self = this; // notice the new position
this.setActive(object.isActive());
}
突然,在测试程序时,我得到:
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
这是怎么回事?为什么在 Gson 完成序列化后初始化 self 会改变一切? gson 是这里的错吗?就代码而言,我指向相同的 this
引用,这是怎么回事?即使解决方案只是将它移动到 fromGson
,如果它没有任何可反序列化的内容(如果配置不存在,这可能会在我的原始代码中发生),它不会调用的情况呢? fromGson
?
实际上,正如@MadProgrammer 提到的,由于 Gson,我对静态的引用被强制更改。由于 Gson 序列化了该值,因此我对 self = this
的引用被修改为 Gson#fromJson
.
的反序列化 MyObject
为了避免这种情况,我添加了一个空检查(个人而言,我不喜欢这种动态的静态关系,并且可能会废弃它):
private static MyObject self = null;
public MyObject() {
this.active = false;
if (self == null) { // prevents Gson from messing with it later on
self = this;
}
}
我有一段相对复杂的代码,它使用 Gson 序列化对象以便为它们保存非常具体的值。除此之外,还有一个特定的对象控制着相当大的代码部分,并且经常访问它,所以为了减少不必要的列表管理来找到它(这些配置对象存储在列表中),我创建了一个静态引用以更快地访问它,但它似乎不适用于该对象。
我已经简化了代码,这里的关系让我感到困惑。如果我使用下面的代码,active
将不会在对 this
:
public static void main(String[] args) {
String json = "{ \"active\": false }";
final MyObject myObject = new MyObject();
final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
myObject.fromGson(gson.fromJson(json, MyObject.class));
for (int i = 0; i < 10; i++) {
myObject.setActive(!myObject.isActive());
}
}
private static class MyObject {
@Expose private boolean active;
private static MyObject self;
public MyObject() {
this.active = false;
self = this; // notice where this line is located
}
public static boolean active() {
return self.isActive();
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
System.out.println("this: " + this.isActive());
System.out.println("self: " + self.isActive());
}
public void fromGson(MyObject object) {
this.setActive(object.isActive());
}
}
测试时,输出:
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
this: true
self: false
this: false
self: false
显然,self
没有改变,即使它应该只是访问 this.isActive()
。现在考虑以下更改:
public MyObject() {
this.active = false;
}
public void fromGson(MyObject object) {
self = this; // notice the new position
this.setActive(object.isActive());
}
突然,在测试程序时,我得到:
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
this: true
self: true
this: false
self: false
这是怎么回事?为什么在 Gson 完成序列化后初始化 self 会改变一切? gson 是这里的错吗?就代码而言,我指向相同的 this
引用,这是怎么回事?即使解决方案只是将它移动到 fromGson
,如果它没有任何可反序列化的内容(如果配置不存在,这可能会在我的原始代码中发生),它不会调用的情况呢? fromGson
?
实际上,正如@MadProgrammer 提到的,由于 Gson,我对静态的引用被强制更改。由于 Gson 序列化了该值,因此我对 self = this
的引用被修改为 Gson#fromJson
.
MyObject
为了避免这种情况,我添加了一个空检查(个人而言,我不喜欢这种动态的静态关系,并且可能会废弃它):
private static MyObject self = null;
public MyObject() {
this.active = false;
if (self == null) { // prevents Gson from messing with it later on
self = this;
}
}