4

I have a pretty general question about java. I want to know if there is a simple way to recreate this c++ code in java:

class A { public: int first; int second; A(const A& other) { *this = other; } ... } 

So basically a copy constructor where you can pass an existing object of A a new object of a in the constructor, and it will copy the contents and build an exact copy of the existing object of A.

trying

class A { int first; int second; public A(A other){ this = other; } ... } 

sadly doesn't work, since eclipse tells me "this" is not allowed on the left handside of an assignment since it's not a variable.

I know I would achieve the same results doing:

class A { int first; int second; public A(A other){ this.first = other.first; this.second = other.second; } ... } 

But I would like to know if there is an easier way, since sometimes you have a few more class variables.

Thanks in advance!

3
  • 1
    You could create a private constructor that takes in all of your variables and have all other constructors call that in some fashion. Commented Jul 11, 2017 at 19:07
  • 1
    You mean a constructor that takes in an instance of that object and then copies its data. Thats probably as simple as it gets. Commented Jul 11, 2017 at 19:08
  • @Daedric excactly - but without having to copy its variables individually. (i was wondering if i had misused the word copy constructor after i submitted the question - Did this lead to confusion?) Commented Jul 11, 2017 at 19:11

3 Answers 3

2

There is no easier way defined by the Java language, however there are some tricky techniques that may allow you to do so:

  1. Clone object via serialization: http://www.avajava.com/tutorials/lessons/how-do-i-perform-a-deep-clone-using-serializable.html : precondition - all properties of the class in structure has to be either primitive or of a classes marked as Serializable
  2. toString() -> fromString(String s) - corresponding methods has to be implemented
  3. POJO and beans can be easily reconstructed using intermediate XML / JSON representation with available libraries like Jackson etc.

To my knowledge the most efficient way after direct mapping is through serialization mechanism.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay thanks a lot, I will have a look at Serialization!
Yeah Serialization is good but it would be more involved as opposed to a simple constructor.
1

What you have in the third version of that class is legal java that does the same thing as your C++ class, however I do not think there is an easier way than what you have written.

Comments

1

Best way to recycle code:

class A { int first; int second; public A(int f, int s){ this.first = f; this.second = s; } public A(A a){ this(a.first, a.second); // id use getters instead ofc. } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.