Passing objects as parameters to methods

  • Methods can have reference type parameter variables.

  • Here is an example of a printCircle() method with an object reference variable as parameter:

        public static void main(String[] args)
        {
            Circle circle1 = new Circle(1);
            Circle circle2 = new Circle(4);
            
            printCircle(circle1);  // Pass reference variable as argument
            printCircle(circle2);  // Pass reference variable as argument
        }
    
        // Method with an object as parameter
        public static void printCircle( Circle c )
        {
            System.out.println("The area of the circle of radius "
                               + c.radius + " is " + c.getArea());
        } 

  • Remember:   In Java, arguments are passed by copying to the parameter variables

Review:   copying reference typed variables creates an alias

  • Assigning to variables of reference type will make an alias:


  • Update using c.radius will also change circle1.radius:

        public static void main(String[] args)
        {
            Circle circle1 = new Circle(5);  
            Circle c = circle1;   // c is alias for circle1
            
            c.radius = 99;
            System.out.println( circle1.radius ); // Prints: 99
        } 

DEMO: demo/10-classes/20-copy-ref/Demo.java

What happens when a method updates its object passed as a parameter

  • Reference variable argument and its (corresponding) formal parameter are aliases
    (i.e.: they point to the same object)

  • Updating instance variables using the parameter will therefore also update the original object:

        public static void main(String[] args)
        {
            Circle circle1 = new Circle(4);
    
    	System.out.println( circle1.getRadius() );  // 4
    	incrementRadius( circle1 );  // Pass ref vara as parameter
    	System.out.println( circle1.getRadius() );  // 5
        }
    
        // Method updates object passed as parameter
        public static void incrementRadius( Circle c )
        {
            c.radius++;   // Update circle1's radius !
        } 

DEMO: demo/10-classes/20-copy-ref/Demo2.java

Notice the difference in behaviors

  • Difference in behavior when passing a primitive type argument and a reference type argument:

     public static void main(String[] args)
     {
         Circle circle1 = new Circle(4);
         int    x       = 4;
    
         System.out.println( circle1.getRadius() + " " + x ); // *** 4 4   <--- 
    
         incrementRadius( circle1 );        // Pass a reference type variable
         incrementInt( x );                 // Pass a primitive type var
    
         System.out.println( circle1.getRadius() + " " + x ); // *** 5 4   <--- 
     }
    
     public static void incrementRadius( Circle c )
     {
         c.radius++;   // Increment c.radius by 1
     } 
    
     public static void incrementInt( int c )
     {
         c++;          // Increment c by 1
     } 
    

DEMO: demo/10-classes/20-copy-ref/Demo3.java

Review:    passing primitive variables to methods

  • For primitive data types, the value of the argument copied (= assigned) to the parameter variable:

    Note:   x in main( ) and c in increment( ) are different variables

Review:    passing primitive variables to methods

  • When increment( ) executes c = c + 1;, it updates the parameter variable c :
     

    The variable x in main( ) is not affected

What happens when you pass a reference type argument

  • The reference type Circle variable x contains a reference to a Circle object:
     

     

What happens when you pass a reference type argument

  • When the reference in the argument is passed (= copied) to the parameter variable, it creates an alias (i.e., the argument and parameter point to the same object):

    Note:   x in main( ) and c in increment( ) both reference to the same Circle object

What happens when you pass a reference type argument

  • When increment( ) executes c.radius = c.radius + 1;, it updates the radius variable through the reference c:

    The variable x.radius in main( ) is ALSO affected because it's the same object !

Comment: we have seen something similar before with arrays (objects !) ---