Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement