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());
    } 

In Java, the object reference in the argument ( circle1 or circle2) is copied to the parameter variable c

Reminder:   assigning (= copying) reference typed variables

  • 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.radius = 99;
            System.out.println( circle1.radius ); // Prints: 99
        } 

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

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

Since the formal parameter is an alias for the actual parameter, the original object will be updated:

    public static void main(String[] args)
    {
        Circle circle1 = new Circle(4);

	System.out.println( circle1.getRadius() );  // 4
	incrementRadius( circle1 );  // increment radius by 1
	System.out.println( circle1.getRadius() );  // 5
    }

    // Method updates object passed as parameter
    public static void incrementRadius( Circle c )
    {
        c.radius++;   // Increment radius by 1
    } 

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

In Java, the formal parameter c is an alias of the actual parameter circle1

So c.radius++ will also updates circle1.radius

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  <---
        }
    
        // Method updates object passed as parameter
        public static void incrementRadius( Circle c )
        {
            c.radius++;   // Increment c.radius by 1
        } 
    
        // Method updates int passed as parameter
        public static void incrementInt( int c )
        {
            c++;          // Increment c by 1
        } 
    

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

Review:    passing primitive variables to methods

  • In Java, 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

  • In Java, the value of the argument copied (= assigned) to the parameter variable.                              

    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 !) ---

Quiz:   what is printed by this program ?

    public static void main(String[] args)
    {
        Circle circle1 = new Circle(4);
	
	System.out.println( circle1.getRadius() ); // Prints  4

	updateCircle( circle1 );
	System.out.println( circle1.getRadius() ); // Prints: ?? <---
    }

   
    public static void updateCircle( Circle c )
    {
        c = new Circle(99);
    } 

DEMO: demo/03-classes/99-quiz

Quiz:   explained....

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

     

Quiz:   explained....

  • In Java, the value of the argument copied (= assigned) to the parameter variable.                              

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

Quiz:   explained....

  • When updateCircle( ) executes c = new Circle(99);, it creates another Circle object and assign its address to reference var c:

    The variable x.radius in main( ) is not affected

Comment:   we can never make x in main( ) reference to a different object using a method call !

(Because x is passed-by-value, we cannot update x (and make it reference to a different object))

Quiz revisted:   what if we DO want to update circle1 ?

Suppose we do want to update circle1 with the object from updateCircle( ):

    public static void main(String[] args)
    {
        Circle circle1 = new Circle(4);
	
	System.out.println( circle1.getRadius() ); // Prints  4

	updateCircle( circle1 );   // Make make update to circle1
	System.out.println( circle1.getRadius() ); // Prints: 99
    }

   
    public static void updateCircle( Circle c )
    {
        c = new Circle(99);


    } 

What can we do ?

Quiz revisted:   what if we DO want to update circle1 ?

(1)   make updateCircle( ) return the new Circle object:

    public static void main(String[] args)
    {
        Circle circle1 = new Circle(4);
	
	System.out.println( circle1.getRadius() ); // Prints  4

	updateCircle( circle1 );   // Make make update to circle1
	System.out.println( circle1.getRadius() ); // Prints: 99
    }

   
    public static Circle updateCircle( Circle c )
    {
        c = new Circle(99);

        return c;   // Step 1 
    } 

Can you figure out what to do next ?

Quiz revisted:   what if we DO want to update circle1 ?

(1)   update circle1 with the return value:

    public static void main(String[] args)
    {
        Circle circle1 = new Circle(4);
	
	System.out.println( circle1.getRadius() ); // Prints  4

	circle1 = updateCircle( circle1 );  // Step 2 
	System.out.println( circle1.getRadius() ); // Prints: 99
    }

   
    public static Circle updateCircle( Circle c )
    {
        c = new Circle(99);

        return c;   // Step 1 
    } 

Done !