Java – Call by reference

Java – Call by reference 2017-08-09T11:05:57+00:00

Java Call by reference :

Here we pass reference as parameter in function calling.

We all know that reference means object so we pass object as parameter.

A reference to an argument (not value of argument) is passed to the parameter.

Inside the subroutine, this reference is used to access the actual argument specified in the call.

This means that changes made to the parameters will affect the argument used to call the subroutine.

When we pass an object to a method, the situation changes, because objects are passed by call-by-reference.

When we create a variable of a class type, we are only creating a reference to an object. Thus,

When you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument.

This effectively means that objects are passed to method do affect the object used as an argument.



Output :
a and b before call…………
a = 10
b = 20
a and b after call………….
a = 20
b = 40
You can see that after calling method value of original a and b is changed because of call by reference.
Here we pass “r” reference (Object) as parameter in method calling. So changes inside method will affect original variable of class.

Prev Next