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 <---
}
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 primitivedata types,
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
When the
reference
in the
argument is
passed (= copied)
to the
parameter variable,
it creates an
alias
(i.e., the
argument
and parameterpoint to the
sameobject):
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 !)
---