Methods can
have reference typeparameter 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 changecircle1.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 variableargument and its
(corresponding)
formal parameter are
aliases
(i.e.: they
point to the
sameobject)
Updatinginstance variables
using the parameter
will thereforealso
update
the originalobject:
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 typeargument
and a reference typeargument:
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/10-classes/20-copy-ref/Demo3.java
Review:
passing
primitive
variables to methods
In Java,
the
value
of the
argumentcopied (= assigned)
to the
parameter variable:
>
Note:x in
main( ) and
c in
increment( )
are
different variables
Review:
passing
primitive
variables to methods
Whenincrement( )executesc = 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 typeCircle variablex contains
a reference to
a Circle object:
What happens when you pass
a reference type argument
In Java,
the
value
of the
argumentcopied (= assigned)
to the
parameter variable.
Note:x in
main( ) and
c in
increment( )bothreference
to the same Circle object
What happens when you pass
a reference type argument
Whenincrement( )executesc.radius = c.radius + 1;,
it updates the
radius variablethrough 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/10-classes/99-quiz
Quiz: explained....
The reference typeCircle variablex contains
a reference to
a Circle object:
Quiz: explained....
In Java,
the
value
of the
argumentcopied (= assigned)
to the
parameter variable.
Note:x in
main( ) and
c in
increment( )bothreference
to the same Circle object
Quiz: explained....
WhenupdateCircle( )executesc =
new
Circle(99);,
it createsanotherCircle 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 objectusing
a method call !
(Because x is
passed-by-value,
we
cannotupdate x
(and make it reference to
a different object))
Quiz revisted:
what if we
DO want to update circle1 ?
Suppose we
do want to
updatecircle1 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)updatecircle1 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
}