Review:    objects in Java

  • In Java, an object consists of 2 components:

    1. An object reference variable   (which is not an object, but references to an object)

    2. The actual object which consists of a number of instance variables

  • Schematically:


  • This storage structure of objects makes it a bit complicated to make a copy of an object

What does it mean to copy an object

  • To copy an object means:

      • Make a duplicate of an object where the duplicate object contains the same data as the original object

      • Furthermore:   updating the instance variables in the duplicate object must not affect the values in the original object

  • Schematically: before making a copy of the object circle1

What does it mean to copy an object

  • To copy an object means:

      • Make a duplicate of an object where the duplicate object contains the same data as the original object

      • Furthermore:   updating the instance variables in the duplicate object must not affect the values in the original object

  • Schematically: after making a copy of the object circle1

How to copy an object in Java    step-by-step instructions

  • Initial state:

        public static void main(String[] args) 
        {
            Circle circle1 = new Circle(4);  // The original object
         
    
            
     
     
        } 

How to copy an object in Java    step-by-step instructions

  • Step 1: create an object to store the copy

        public static void main(String[] args) 
        {
            Circle circle1 = new Circle(4);  // The original object
          
            Circle circle2 = new Circle();
    
      
     
        } 

How to copy an object in Java    step-by-step instructions

  • Step 2: copy the instance variables from the original to the copy object

        public static void main(String[] args) 
        {
            Circle circle1 = new Circle(4);  // The original object
          
            Circle circle2 = new Circle();
    
            circle2.radius = circle1.radius; 
    
        } 

 

How to copy an object in Java    step-by-step instructions

  • Result: updates on the object copy will not affect the original object:

        public static void main(String[] args) 
        {
            Circle circle1 = new Circle(4);  // The original object
          
            Circle circle2 = new Circle();
    
            circle2.radius = circle1.radius; 
    	circle2.radius++; // Will not affect circle1.radius
        } 

Demo: demo/10-classes/10-copy/Demo.java + Circle.java

A common error in copying objects

  • Variables of primitive data types (such as int, double, etc) can be copied with an assignment statement:

        int x = 4;   // int is a primitive data type
        int xCopy;
    
        xCopy = x;   // Copies x to xCopy 

  • However, in Java, the assignment statement will not copy objects of non-primitive data types:

        Circle circle1 = new Circle(4);  // Non-primitive data type
    
        Circle circle2;
    
        circle2 = circle1;   // Does not copy an object 
                               // ** This copies the reference
    			   // ** in circle1 to circle2

Review:   the effect of assignment with a reference variable

  • Initially we have the object circle1:
     

        Circle circle1 = new Circle(4);
    
    
       
         

    Schematically:

     

Review:   the effect of assignment with a reference variable

  • The definition Circle circle2 defines another reference variable:                                        

        Circle circle1 = new Circle(4);
    
        Circle circle2;
    
     

    Schematically:

     

Review:   the effect of assignment with a reference variable

  • The assignment circle2 = circle1 will make circle2 reference to the object that circle1 is referencing:

        Circle circle1 = new Circle(4);
    
        Circle circle2;
    
        circle2 = circle1;   // Does not copy an object object  

    Schematically:

    I.e.: circle1 and circle2 are aliases

Review:   the effect of assignment with a reference variable

  • Because circle2 and circle1 reference to the same (Circle) object, updates made using circle2.radius will also affect circle1.radius and vice verse:

        public static void main(String[] args) 
        {
            Circle circle1 = new Circle(4);
    
            Circle circle2;
    
            circle2 = circle1;   // Does not copy an object object
    
    	circle2.radius = 999; // Update will also affect myList[1]
    
    	System.out.println( circle1.radius );  // Changed !
    	System.out.println( circle2.radius );
        }  

    The fact that circle1.radius is change proves that circle1 and circle2 are aliases

Demo: demo/10-classes/10-copy/Demo2.java + Circle.java