0

What happens when I initialize one object to another in java? Is the reference of that object copied in new object or is a new object created with same member values as in the orginal object;

A obj1=new A("apple");
A obj2=obj1;

What is the correct interpretation for this scenario?

  • 1
    There's only 1 object in memory and two variables: `obj1` and `obj2` hold a reference to that object in memory. In other words, both `obj1` and `obj2` refer to the _same_ object – Yousaf Apr 26 '22 at 07:12
  • 1
    Hint: when you are new to a language ... it is VERY safe to assume that any question you can think of has been asked before. Here, and in many other places. So please do some research before posting here. Don't ask people to write down something AGAIN that was written down many many times before. – GhostCat Apr 26 '22 at 07:39

2 Answers2

2

when I initialize one object to another in java

I do not know what you mean by "to another". But you initialized only a single object.

First line

Regarding your first line:

A obj1=new A("apple");

A obj1 declares a reference variable, giving it a name obj1, and specifies that this var will hold a reference to any object of type A. This reference variable is initially null.

Calling new A("apple") constructs an object some place in memory. The result, the return value, of this code is a reference to the new object.

The = assigns that reference to be the content of the variable named obj1.

You can think of a reference variable as containing the address in memory where the beginning of that object's block of allocated memory resides. In Java we never see the literal contents of a reference variable. But we know we can reach the object via that reference variable.

In our daily work of programming in Java, we may generally think of obj1 as being the object. But actually obj1 is a way to find the object, a tether attached to the object, a line we can follow to access the object somewhere else in memory.

Second line

Regarding your second line:

A obj2=obj1;

First you declare a new variable named obj2 to hold a reference to an object of type A. Then you copied the reference from obj1 and put that copy into obj2.

You are left with:

  • A single object.
  • Two references to that single object.

diagram of two reference variables leading to the same object in memory

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

It is just the reference coppied, not an actual new object in some other memory space.

Normally for deep-copy you can declare yourself a clone method in that class that uses the properties of the passed object and creates another object with new keyword and returns it. This way you will have a new object when you use clone method. More specifically you can declare your class to implement Cloneable interface and then provide an override implementation for the method clone() which already exists in parent Object class.

For shallow-copy you should again create a clone method in your class and in this method you can just use return super.clone() so that the default clone() method provided by Object class will be used to make a shallow-copy of the object meaning only primitive fields will be actually copied and for any non primitive fields the reference will be copied instead.

For your simple example where the field in this class is only some String you can use the shallow copy of clone already provided by Object class and this will seem enough.

If however you had more non primitive fields in this class, then you will had to override your clone method and provide some implementation so that a deep copy could be returned.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47