Elmer
Elmer

Reputation: 75

I'm having trouble understanding how object references work in Java?

The way I understood references originally was that they were simply memory references that held the memory location of the actual object they hold. The code below and its output confuses that for me, though. Here you can see the implementation of a simple class Man.

I create a Man object in the first line with the reference being called peter. peter on its own is just a memory location, right? So person should just be storing the object in the memory location it is at.

But when I assign another Man reference to peter and later change peter's name, person does not know this and prints the first name. How can this be since it stores the memory reference for peter? Shouldn't it be able to follow changes made to it?

public class Testing {

  public static void main(String[] args) {
    Man peter = new Man("brown", 182, 78000, "Peter");
    Man person = peter;
    peter = new Man("brown", 182, 78000, "Leonard");
    System.out.println(person.name);
  }
}

class Man {

   String hairColor;
   int height;
   double salary;
   String name;

   public Man()
   {
     hairColor = "brown";
     height = 180;
     salary = 50500.5;
     name = "John";
   }
   public Man(String hair, int high, double pay, String nam)
   {
        this.height = high;
        this.hairColor = hair;
        this.salary = pay;
        this.name = nam;
   }
}

Upvotes: 1

Views: 1620

Answers (7)

With peter=new Man() creates the new memory location.Now 2 memory location has been created , lets say 100 and 200. Still person is pointing to peter memory location 100. so that it is displaying first constructor name peter.

if you wants to display the nam value as Leonardm, add below line assignment

person = Peter;

System.out.println(person.name);

Upvotes: 0

GhostCat
GhostCat

Reputation: 140407

Here:

Man peter = new Man("brown", 182, 78000, "Peter");

creates a Man object named "Peter".

Man person = peter;

creates another variable "pointing" to the object created above.

peter = new Man("brown", 182, 78000, "Leonard");

creates another Man named Leonard, and afterwards the peter variable "points" to that new, second object.

Note: person didn't "point" to peter. It points to the Man "object" in memory.

And putting another "memory address" into the peter variable doesn't change the initial object you created.

Upvotes: 6

Yehuda Zilberfarb
Yehuda Zilberfarb

Reputation: 1

it works like this:

Man peter = new Man("brown", 182, 78000, "Peter");

means you got new place in memory that has those values.

Man person = peter;

means person has same Pointer to the memory of peter.

peter = new Man("brown", 182, 78000, "Leonard");

you changed the pointer area to new place with new values.

Upvotes: 0

HS447
HS447

Reputation: 81

You are pointing both the Objects to same memory/location then you are assigning the another reference to the 1st object so it will not affect the old memory data.

Upvotes: 0

rghome
rghome

Reputation: 8819

Think of the references as memory addresses. I hope this example explains:

Man peter = new Man("brown", 182, 78000, "Peter");
// Create a new Man object which is placed in (for example) memory location 100
// Assign 100 to peter


Man person = peter;
// Assign 100 to person (copying it from peter)

peter = new Man("brown", 182, 78000, "Leonard");
// Create a new Man object which is placed in memory location 120
// 120 is assigned to peter

System.out.println(person.name);
// person still contains 100, so this prints out the details of the first object

Upvotes: 1

Mark van Turnhout
Mark van Turnhout

Reputation: 66

When you use

= new Man

You create a new object. So peter is watching Man 1, person is watching a new Man 2

Upvotes: 0

Nikola Nikolic
Nikola Nikolic

Reputation: 160

Man peter = new Man("brown", 182, 78000, "Peter");
Man person = peter;
peter = new Man("brown", 182, 78000, "Leonard");

First line assigns a reference to the object with name "Peter", second line assigns previous object reference to variable person.

In the third line you create a new object and assign a reference to it to variable peter, which is completely new reference, while the variable person still keeps the reference of the previous object.

If you wanted to have name "Leonard" in both variables, instead of creating a new object, you could just have in the third line

person.name = "Leonard";

Upvotes: 0

Related Questions