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:
a = Gson.createAnotherA()
It doesn't work.
- 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.
clone()
make me feel even worse.
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.