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
|
DEMO: demo/03-classes/20-copy-ref/Demo.java
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
|
DEMO: demo/03-classes/20-copy-ref/Demo3.java
|
|
|
|
|
Comment: we have seen something similar before with arrays (objects !) ---
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
|
|
|
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))
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 ?
(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 ?
(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 !