5920

How to make a variable to indicate another object?

Question:

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:

<ol><li>a = Gson.createAnotherA()</li> </ol>

It doesn't work.

<ol><li>I tried to copy every field from b to a</li> </ol>

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.

<ol start="2"><li>clone()</li> </ol>

make me feel even worse.

<ol start="3"><li>a = loadA(a);</li> </ol>

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.

Answer1:

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; } }

Answer2:

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; } }

Recommend

  • kotlin convert json array to model list using GSON
  • Using GSON to parse Json into a JAVA Object where the Json Elements may vary
  • How to parse nested JSON with GSON
  • fromJson raises unparseable date exception
  • Most efficient way to move table rows from one table to another
  • How to autopopulate a field in SugarCRM form
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Loading .coffee files via a view in Rails
  • OOP Javascript - Is “get property” method necessary?
  • Create DicomImage from scratch using Dcmtk
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Functions in global context
  • Play WS (2.2.1): post/put large request
  • Linq Objects Group By & Sum
  • Retrieving value from sql ExecuteScalar()
  • Master page gives error
  • Different response to non-authenticated users and AJAX calls
  • Trying to switch camera back to front but getting exception
  • How to set my toolbar fixed while scrolling android
  • Redux, normalised entities and lodash merge
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • XCode can't find symbols for a specific iOS library/framework project
  • AT Commands to Send SMS not working in Windows 8.1
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Error creating VM instance in Google Compute Engine
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • Sorting a 2D array using the second column C++
  • How to load view controller without button in storyboard?