Home > Net >  why a second reference to an object does not alter the content in java?
why a second reference to an object does not alter the content in java?

Time:12-12

In the below code, I have created 3 instances of the Movie class. I have assigned some data to the three objects. Secondly, I have created a second reference to the object movie1(movie4). I have also assigned values to attributes of movie4. I printed the new attributes of movie 1 but the contents have not been altered. Why does this happen? What is the exact reason? I know other ways of updating content but I want to know why the second referencing does'nt work.
The content is movie1 is still the same. Why ?

class Driver{

    public static void main(String[] args){
        
        Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
        Movie movie2 = new Movie("The Godfather", 1972, 9.2);
        Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);

        **Movie movie4 = movie1;
        movie4 = new Movie("The Return of the King", 2003, 8.9);**

        movie4.name = "The Return of the King";
        movie4.year_of_release = 2003;
        movie4.rating = 8.9;
        System.out.println("\n*********New Movie 1 Details*************\n");
        movie1.printDetails();

    }
}

CodePudding user response:

After the execution of this command

Movie movie4 = movie1;

The value of movie4 is the reference to movie1, So the changes made on movie4 would be reflected in movie1 at this point.

movie4 = new Movie("The Return of the King", 2003, 8.9);

But after this command, movie4 no longer holds the reference to movie1, its value is an entirely new reference. So any changes made are only made on the new object that movie4 is pointing to not on the movie1

CodePudding user response:

This made movie4 point to movie1:

Movie movie4 = movie1;

but, then you immediately updated movie4 to point to a different object:

movie4 = new Movie("The Return of the King", 2003, 8.9);

So at that point, movie1 and movie4 are no longer pointing to the same Movie, so changes you make via movie4 don't affect the object movie1 points to.

If you had changed the object without replacing movie4:

Movie movie4 = movie1;
movie4.name = "The Return of the King";

...then you would have seen that change to the object whether you looked at movie1.name or movie4.name.

A bit more detail:

After you did this:

Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
Movie movie2 = new Movie("The Godfather", 1972, 9.2);
Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);

...you had something like this in memory (omitting a lot of details):

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie1:Ref4516−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Shawshank..." |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie2:Ref8469−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Godfather"    |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie3:Ref4789−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Dark Knight"  |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

Then you did this:

Movie movie4 = movie1;

...making movie4 and movie1 point to the same movie:

                  
movie1:Ref4516−−− 
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
                  −−−>|         (Movie)          |
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie4:Ref4516−−−     | name: "The Shawshank..." |
                      | ...                      |                               
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie2:Ref8469−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Godfather"    |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie3:Ref4789−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Dark Knight"  |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

So far, so good, but then you did:

movie4 = new Movie("The Return of the King", /*...*/);

...which made movie4 point to a new object:

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie1:Ref4516−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Shawshank..." |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                                                 
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie2:Ref8469−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Godfather"    |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie3:Ref4789−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Dark Knight"  |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie4:Ref9546−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Return of..." |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

So assigning to movie4.name just changes that new object, not the one that movie1 points to.

If you didn't do that movie4 = new Movie(/*...*/) bit, so movie1 and movie4 still pointed at the same object

                  
movie1:Ref4516−−− 
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
                  −−−>|         (Movie)          |
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie4:Ref4516−−−     | name: "The Shawshank..." |
                      | ...                      |                               
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie2:Ref8469−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Godfather"    |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie3:Ref4789−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Dark Knight"  |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

...then assigning to movie4.name would change the object that both movie4 and movie1 are pointing at:

movie4.name = "The Return of the King";
                  
movie1:Ref4516−−− 
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
                  −−−>|         (Movie)          |
                 |     −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie4:Ref4516−−−     | name: "The Return of..." |
                      | ...                      |                               
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie2:Ref8469−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Godfather"    |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 

                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
movie3:Ref4789−−−−−−−>|         (Movie)          |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
                      | name: "The Dark Knight"  |
                      | ...                      |
                       −−−−−−−−−−−−−−−−−−−−−−−−−− 
  • Related