0

Say I have a class Dog and I create two instances of Dog like this:

Dog dog1 = new Dog();

Dog dog2 = dog1;

If I use

dog1.shower();

will dog2 also perform the call? And if so, how do I prevent dog2 from calling the same methods?

Manuel
  • 3,828
  • 6
  • 33
  • 48
xheyhenry
  • 1,049
  • 2
  • 13
  • 25

7 Answers7

4

By writing Dog dog2 = dog1; you declare a reference dog2, which will point to the same object as dog1.

So, calling either dog1.shower(); or dog2.shower(); will result in the same behavior (calling the shower() method on the same Dog object).

If you write

Dog dog2 = new Dog();

you make your dog2 refer to a different, newly created Dog object.

Edit
If you need dog2 to reference a different Dog and get all the qualities of dog1, you have options :

  • Create a copy constructor as chrylis suggested. After you do this you can write:

    // dog2 refers to new copy of `Dog`, identical to original dog1
    Dog dog2 = new Dog(dog1); 
    
  • Override Object.clone() in your class Dog. In this case you will have:

    // dog2 refers to new copy of `Dog`, identical to original dog1
    Dog dog2 = dog1.clone(); 
    

I think you could also refer to Edgar's answer with simplistic examples of these options.

Community
  • 1
  • 1
kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • If I did Dog dog2 = new Dog(); and then did dog2 = dog1, would that cause the same problem? Or would dog2 then get all the qualities of dog1 but then reference a different Dog? – xheyhenry Nov 30 '13 at 10:13
  • no, you will have same problem. When you do `dog2 = dog1;` you will re-assign the reference and dog2 will again point to exactly same object as dog1 – kiruwka Nov 30 '13 at 10:14
  • @xheyhenry Using the assignment operator `=` makes the variable point to a specific instance of `Dog`. If you say `Dog dog2 = new Dog(); dog2 = dog1;`, then `dog2` now points to the same `Dog` as `dog1`, and the `new Dog()` you put in `dog2` is discarded. If you want to make a copy of a `Dog` with all its properties, you need to write a **copy constructor**. – chrylis -cautiouslyoptimistic- Nov 30 '13 at 10:15
4

This is what happens:

Dog dog1 = new Dog();

enter image description here

Dog dog2 = dog1;

enter image description here

When you call dog1.shower();, a single shower happens, because it happens on the same object.

enter image description here

You should create another instance of dog Dog dog2 = new Dog();, so they won't end up showering the same dog.

enter image description here

Jops
  • 22,535
  • 13
  • 46
  • 63
2

in your case dog2 just "points" to dog1. Whatever you change in dog1 (since it is the same object), will change in dog2, and vice-versa.

What you might wanna do is implement a copy function for your dog, if you want to have a dog with the same attribute values, but that is not the same object:

 public Dog clone() {
     Dog dog = new Dog();
     // now write all the attributes from the current dog, into this new dog
     return dog;
 }

And then create dog2 like this:

 dog2 = dog1.clone();

You could also move it into the constructor of Dog:

 public Dog(Dog dogToClone) {
     // here take all attributes from dogToClone and add them to this dog.
 }

And your call would be:

 dog2 = new Dog(dog1);
Blub
  • 3,762
  • 1
  • 13
  • 24
1

In your example dog2 doesn't exist as separate dog, it is the same dog as dog1. So this same dog would take shower, and will take shower only once.

croraf
  • 4,332
  • 2
  • 31
  • 50
0

Simply do not assign Dog dog2 = dog1; rather make a new object and reassign all the properties of dog1 in dog2.

Dog dog2 = new Dog();
//Assign all the properties of dog1 to dog2 manually

When you assign dog2 = dog1 it does not mean you are creating a new object rather you are just creating a reference to same object i.e. both the objects are same.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
0

In your case dog1 and dog2 are referring to the same Dog object. dog1 and dog2 are references on stack that are pointing to an Object which resides on heap. So calling any one will modify the single Dog Object residing on heap. IF you don't want this create a new object for Dog 2 and copy the contents of dog1 to dog2 one by one.

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46
0

dog 1 is referencing to an instace of Dog() and dog 2 is also referencing to same instance so overall alternating one may alternate the other. if you don't want to let any dog interfere with the other one, you may make in each variable a new instance of Dog

     Dog dog1=new Dog(), dog2= new Dog();

this may solve your problem.

aniruddha
  • 61
  • 5