Java – Call by value

Java – Call by value 2017-08-09T11:03:18+00:00

Now we all know that how to define and call the methods.

There are two types of calling method and those are

1. call by value

2. call by reference

Here we illustrate call by  value and in next topic we will look at call by reference.

In call by value when we call any method we pass value as method parameter so changing in local variables of the method doesn’tt affect the original variables of class.

This method copies the value of an argument into the formal parameter of the subroutine.

Therefore, changes made to the parameter of the subroutine have no effect on the argument.

In java, when we pass a primitive type to a method, it is passed by value.

Thus, what occurs to the parameter that receives the argument has no effect outside the method.

EX :



Output :
a and b before call............
a = 10
b = 20
a and b after call............
a = 10
b = 20
You can see that after calling method we change value of a and b but it will not afect the original value of class members because of call by value.
We pass value v.a and v.b as parameter and it will change local method`s a and b variables.

Prev Next