-2

I am a beginner, and I wonder about the following.

Let's say I have a class foo and created an object foo first and put some data into it:

foo first = new foo();
first.data="mydata";

I then want to create another object foo second and make it have the same contents (but not identical memory location) as first. Can I do the following to achieve that?

foo second = new foo();
second = first;

Or does this just set first and second to the same memory address and I just effectively obtain two handles on a single object in memory?

I read about cloning of objects and it seems way too complicated. I feel like since I went through the trouble of defining a new foo in foo second = new foo(); the compiler should realize that second = first; intends to copy the entire thing and not just point to the first one. Is that how it works?

So my question is: Why is this not how this works? What is the flaw in the above logic of having the compiler set up like that?

Kagaratsch
  • 963
  • 8
  • 18
  • 2
    "_I read about cloning of objects and it seems way too complicated_" - well, I'm afraid to say, you're going to have to learn more about it. Although I would recommend a [copy ctor](http://www.javapractices.com/topic/TopicAction.do?Id=12) over `clone()`. I would also recommend you use [accessors/mutators](http://stackoverflow.com/a/15711958/2071828) over direct variable access. – Boris the Spider Feb 08 '17 at 13:34
  • *"Is that how it works?"* If that would be the case, then why do you think the other users write "complicated" stuff to clone their objects? – Tom Feb 08 '17 at 13:34
  • @Tom goot point! I guess my question is rather about "why is this not how it works"? – Kagaratsch Feb 08 '17 at 13:35
  • In order to be able create a clone of an object, you should first implement the [Cloneable](https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html) interface in the class definition of the class you wish to clone, and override the clone method. An example can be seen [here](http://www.javatpoint.com/object-cloning) – ZombieTfk Feb 08 '17 at 13:35
  • "_why is this not how it works?_" - because what you suggest is, frankly, not workable. i.e. there is no way in which your proposed solution makes any sense whatsoever. – Boris the Spider Feb 08 '17 at 13:36
  • 1
    When you do `second = first;` you're just copying the reference (pointer, address) of `first` and store the reference in `second`, essentially losing any way to access your just created second instance. – Mark Jeronimus Feb 08 '17 at 13:37
  • @MarkJeronimus i see. – Kagaratsch Feb 08 '17 at 13:38

1 Answers1

3

Best way is to use a copy constructor.

When you see talk about clone() being intended for this, note that it's very controversial and discouraged (Google if you want to know why). The notable exception is: arrays!

A copy constructor works like this:

Foo first = new Foo();
first.data="mydata";

Foo second = new Foo(first);

In Foo's constructor that takes another Foo, just copy the fields, using their copy contructors too if they are complex objects.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50