3
public class TestVO {

    public static void main(String[] args) {
        VO vo1 = new VO();
        VO vo2 = new VO();
        VO vo3;
        VO vo4 = new VO();

        vo1.setName("Sourav");
        vo2.setName("Anil");

        vo3 = vo1;
        vo4 = vo1;

        System.out.println(" " + vo4.getName());

        vo1.setName("Abhishek.");

        System.out.println(vo1.getName() + "  " + vo2.getName() + " " + vo3.getName() + " " + vo4.getName());
    }
}

--OUTPUT is:---

Sourav

Abhishek.  Anil Abhishek. Abhishek.

VO is a simple class contain String name and contain getter and setter.

In the first System.out.println *vo4.getName()* print : Sourav ok.It's fine.

But in the 2nd System.out.println *vo4.getName()* print : Abhishek.

My Question is why 2nd print is Abhishek.. I have kept a copy of vo1 object in vo4 object not a reference. That means it create a new memory. vo1 and vo4 are different.Then why vo4.getName is changed in the 2nd time. I am setting the vo1 object but vo4 is automatically changed. Why it is happening ??

1
  • 1
    You create objects with the new keyword, = transfers the reference of that object into the variable, equally var1=var2 copies the reference not the object Commented Jul 12, 2013 at 7:56

8 Answers 8

26

I have kept a copy of vo1 object in vo4 object not a reference.

No, you've pointed the variables vo1 and vo4 at the same object, like this:

+-----+
| vo1 |--------\
+-----+         \    +----------------+
                 --->| (object)       |
+-----+         /    | name = Abishek |
| vo4 |--------/     +----------------+
+-----+

Let's follow the code through:

VO vo1 = new VO();

Gives us:

+-----+              +----------------+
| vo1 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

Then:

VO vo2 = new VO();

Now we have:

+-----+              +----------------+
| vo1 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

+-----+              +----------------+
| vo2 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

Then:

VO vo3;

...which just creates vo3 with null (not pointing at any object).

Then:

VO vo4 = new VO();

So we have:

+-----+              +----------------+
| vo1 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

+-----+              +----------------+
| vo2 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+
+-----+
| vo3 | (is null)
+-----+

+-----+              +----------------+
| vo4 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

Now:

vo1.setName("Sourav");
vo2.setName("Anil");

Gives us:

+-----+              +----------------+
| vo1 |------------->| (object)       |
+-----+              | name = Sourav  |     *** change is here ***
                     +----------------+

+-----+              +----------------+
| vo2 |------------->| (object)       |
+-----+              | name = Anil    |     *** and here ***
                     +----------------+
+-----+
| vo3 | (is null)
+-----+

+-----+              +----------------+
| vo4 |------------->| (object)       |
+-----+              | name = null    |
                     +----------------+

Here's where things get interesting:

vo3 = vo1;
vo4 = vo1;

That points vo3 at the same object vo1 points to, and points vo4 at that object as well, releasing the object vo4 used to point to (which becomes eligible for garbage collection). Giving us:

+-----+
| vo1 |----\
+-----+     \
             \
+-----+       \      +----------------+
| vo3 |------------->| (object)       |
+-----+       /      | name = Sourav  |
             /       +----------------+
+-----+     /
| vo4 |----/
+-----+

+-----+              +----------------+
| vo2 |------------->| (object)       |
+-----+              | name = Anil    |
                     +----------------+

Now

System.out.println(" " + vo4.getName());

...gives us "Sourav" as you'd expect.

Then

 vo1.setName("Abhishek.");

...changes the object that vo1, vo3, and vo4 are all pointing to:

+-----+
| vo1 |----\
+-----+     \
             \
+-----+       \      +----------------+
| vo3 |------------->| (object)       |
+-----+       /      | name = Abishek |
             /       +----------------+
+-----+     /
| vo4 |----/
+-----+

+-----+              +----------------+
| vo2 |------------->| (object)       |
+-----+              | name = Anil    |
                     +----------------+

...and so getName() on vo1, vo3, or vo4 will give you "Abishek."

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

1 Comment

@GrahamSmith: Love me some ASCII-art. I probably should have used yuml.me, though. :-)
6

Because both vo1 and vo4 are referring to the same object.

Below assignment makes vo4 to refer to an object that is referenced by vo1.

vo4 = vo1;

And hence, any changes made to referring object by any of the reference(be it vo1 or vo4) will be reflect to the same object.

So if you change say vo4.setName("xyz"), its going to change the object which is referred by vo4 but actually vo1 is also referring to the same object. So changes are made at one memory location allocated for ONE object which is being REFERENCED by many references( in this case vo1 and vo4).

Comments

2

You are assigning the same object reference to vo1 and vo4 .

vo4 = vo1;

Now both the reference variables are pointing to the same object.

enter image description here

Comments

1

Object is an entity which will store actual value for the fields defined within it.Object will get created on memory using new keyword and reference will be returned to reference variable.Reference variable is not an object. So in your example new VO() will create object on heap memory and VO1 will have reference to it.

Comments

0

when you do vo4 = vo1; you simply copy vo1 by refernce to vo4, in other terms you do a pointer assignment so they both assign to the same location in memory which results if vo1 changes it will also affect vo4 too, as in the result you see

Comments

0

Everything in java except for the basic data types is a pointer. After creating vo4 you set it equal to vo1. Now vo4 points to v01.

Comments

0

When you do vo4 = vo1, it means the reference variable vo4 is referring to the object referred by vo1. So both vo1 and vo4 will refer to same object

Read the first part of this tutorial. You don't need to go till the end as it is about garbage collection but it explains in good detail how refrences are assigned to objects and how are they chagned.

http://www.thejavageek.com/2013/06/22/how-do-objects-become-eligible-for-garbage-collection/

Comments

0

I have to prepare the following picture: as you see vo1, Vo3, Vo4 point to the same object

enter image description here

3 Comments

Simplest diagram to understand.Thankx.
@ABasak: Except the labels are backward. The variables are on the stack, not the heap; the objects are in the heap, not the stack.
@ T.J. Crowder: Thank you very much for your comment, I corrected this error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.