-1

I'm a beginner so it really doesn't get any simpler than this.
I got 2 separate .java files in a folder.
A class responsible for the "attributes" of a dog (name, age, breed etc).

   ...
   public String name;
   public byte age;
   public String breed;

      public String makeNoise()
      {
         return "Woof...";
      }

      public String toString()
      {
         return "Name: " +name +"\n" +"Age: " +age +"\n" +"Breed: " +breed;
      }
      ...

And a Tester to test the dog class.

  ...  
  Dog d1 = new Dog();
  d1.name = "Frodo";
  d1.age = 4;
  d1.breed = "Alsatian";

  Dog d2 = new Dog();
  d2.name = "Labby";
  d2.age = 3;
  d2.breed = "Labrador";

  Dog d3 = d2;
  d3.age = 6;
  d3.name = "Mike";


 System.out.println(d1.name);
 System.out.println(d2.age);
 System.out.println(d3.age);
 System.out.println(d2.name);

 System.out.println(d1.age == d2.age);
 System.out.println(d1 == d2);
 System.out.println(d1 == d3);
 System.out.println(d2 == d3);
 ...

The output of

System.out.println(d2.age);

Is returning 6, for some reason, and I don't understand why. Any help would be much appreciated.

Mano
  • 125
  • 9
  • as a side note, never ever compare objects, as `d1 == d2` with the `=` operator, unless you want to check if they refer to the same instance. override [Object#equals](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) and make use of the `equals` functions to determine logical equality. – SomeJavaGuy Aug 22 '16 at 08:14
  • Refer to this question for more details : http://stackoverflow.com/questions/12072727/duplicating-objects-in-java – Ravindra babu Aug 22 '16 at 09:12

5 Answers5

5

When you make the assignment

Dog d3 = d2;

You are copying a reference of a Dog object from the d2 variable to the d3 variable. You are not making a copy of the object.

Therefore, after the assignment, both d3 and d2 refer to the same Dog object. Therefore d3.age = 6; causes d2.age to be 6.

Eran
  • 387,369
  • 54
  • 702
  • 768
2

In Java, every variable (except primitives) is actually pointer. So what you are really doing here is next:

  1. Dog d1 = new Dog(); This makes new instance of dog and d1 points to it.

  2. Dog d2 = new Dog(); This makes another instance of dog and d2 points to it.

  3. Dog d3 = d2; Here you say that d3 is pointer to an instance of dog and in same line you say that instance is same as instance that d2 points to.

  4. d3.age = 6; d3.name = "Mike"; This two lines are making changes on Dog instance that d3 (and d2) is pointing to.

If you wanted to make a copy of instance (create new instance with same values of attributes) you should write copy constructor or implement Cloneable interface and use clone() method.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
1
Dog d3 = d2;
d3.age = 6;

Here you have set the age of d2 to 6, because you have d2 and d3 both referring to the same object. This is how the assignment operator = works in Java.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

Since

Dog d3 = d2

d2 and d3 are referencing to the same object. As you change d3.age, it will also change d2.age.

Not only age, but also name and breed will be changed for both d2 and d3. If you check d2.name, you will get "Mike", not "Labby".

Shahid
  • 2,288
  • 1
  • 14
  • 24
1
Dog d3 = d2;

This means that d2 and d3 are the same object / point to the same instance.

d3.age = 6;

This means that d2.age is 6

Tobias Otto
  • 1,634
  • 13
  • 20