0

I don't have a better title for this question, you may help me change it.

class Example {

    class A {
        private String str;

        public String getStr() {
            return str==null ? "nothing" : str;
        }

        public void setStr(String str) {
            this.str = str;
        }
    }

    public static void main(String[] args) {
        A a = null;
        loadA(a);
        System.out.println(a.getStr());
    }

    private static void loadA(A a) {
        // a = ****;
        // in my project, a is load from a json string using Gson
        // Gson cannot modify a Object, it can only create a new one, say b
        // I tried to copy every field from b to a
        // but the method getStr do not always return the true str
        // so I may have to create another method "getTrueStr()" which feels bad
    }

}

Considered solutions and problems:

  1. a = Gson.createAnotherA()

It doesn't work.

  1. I tried to copy every field from b to a

but the method getStr() do not always return the true str, so I may have to create another method getTrueStr() which make me feel bad.

  1. clone()

make me feel even worse.

  1. a = loadA(a);

this one is good. But I don't very like it. Because I have other loadB(), loadC() and they don't need this syntax, and it will look just inharmonious. If no better solution comes up, I'd choose this one.

Question is, what can I do if I choose none of my given solution.

or

If I am given a reference A a in a method, how can I make it the same to another Object while not using clone() and what I wrote above.

bijiDango
  • 1,385
  • 3
  • 14
  • 28
  • Do you want *one* method to load instances of `A` *and* `B`? –  Mar 13 '15 at 09:24
  • @LutzHorn I don't quite understand you. B?? – bijiDango Mar 13 '15 at 09:27
  • 2
    Probably you should enunciate a question. There is no single question in your question. You just tell us about your feelings. So you want to create a copy/clone of your object of type A? – isnot2bad Mar 13 '15 at 09:27
  • Well, I don't understand your quesiton. What would be the difference between `loadA` and `loadB`? –  Mar 13 '15 at 09:28
  • What do you mean with getStr() doesn't return true str? Do you mean it doesn't return a new String object with the same content? – BeWu Mar 13 '15 at 09:29
  • 1
    @isnot2bad Question is, what can I do if I choose none of my given solution. – bijiDango Mar 13 '15 at 09:31
  • Please first tell us what you are trying to do. It is very hard to understand your question. –  Mar 13 '15 at 09:32
  • @bijiDango Seems I missed your question due to the missing question mark. But still your situation is unclear as you did not define your problem statement properly. Do you want to create a copy of an object? Do you want to copy the fields to an existing object of same or other type? Please update your question! – isnot2bad Mar 13 '15 at 10:32

2 Answers2

2

I think your problem really lies in your getStr method, which is not a real getter, it contains some logic. Is it possible to handle this outside class A?

As far as I understand, you want to create object b (of type A), which is copy of existing instance a.

Because a is loaded from JSON, you could just read the JSON again, which should create a new instance. I guess for some reason you cannot do this.

Instead of using JSON serializer or clone method (which shouldn't be used at all - it has multiple flaws), you could create a "copy-constructor":

class A {

    private String str;

    public A() {}

    public A(A that) {
        this.str = that.str;
    }

    public String getStr() {
        return str==null ? "nothing" : str;
    }

    public void setStr(String str) {
        this.str = str;
    }

}
Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
  • Copy-constructor is awesome. I should have thought of it. Actually I don't want to "create object b" like you said. The fact I have to create a `b`, then make `a` somewhat a "copy" of `b`. – bijiDango Mar 13 '15 at 09:45
  • I thought twice, no, your solution is not capable. If I do something like `a = new A(b)`, the `a` in `main()` won't update to the new object so I can't print it. – bijiDango Mar 13 '15 at 09:53
  • Sorry? I don't understand. What do you mean "it won't update to the new object"? You call `new A(...)`, you assign it to `a`, so `a` is now a new instance. What are you trying to achieve? – Jaroslaw Pawlak Mar 13 '15 at 09:55
  • If I call `a = new A(...)` in the method, it won't change the `a` in `main()`. – bijiDango Mar 13 '15 at 09:59
  • Of course it won't. Java is `pass-by-value`, not `pass-by-reference`. `a` is the address of some object, when you pass `a` to some method, you get the value of `a`, not reference to `a`. `a` in your method is the local copy of that address, you cannot change where the outer `a` points to. – Jaroslaw Pawlak Mar 13 '15 at 10:09
0

I came out with this. Perfectly satisfied my need and looks nice.

class A {

    private A reference;

    private String name;

    public A() {
        reference = this;
    }

    public void setReference(A ref) {
        reference = ref;
    }

    public void setName(String name) {
        reference.name = name;
    }

    public String getName() {
        return reference.name;
    }

}
bijiDango
  • 1,385
  • 3
  • 14
  • 28