Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.

There are two ways to call a method or we can say there are two ways to passing input parameters via a method. Call by value and Call by reference.

         However, I have done research on the web and found that JAVA only Supports Call by Value and not Call by Reference. But I think that logically we have both the methods available in JAVA.

Call By Value or Passing Value via Method

         First, we understand the Call by Value process or Passing Value process, both are same. In this process we pass only values via method parameters and all the changes have been done only in local variables within the called method without affecting original variable values.

Call By Value

             In the above example, we can see that value of a has been passed via method changeValue(a). Method changeValue(int x), assigns 15 to x but the value of a doesn’t get changed in this process.

Related Links:

OOPs Concept:

Java Question And Answer:

Code Example:

public class CallByValueExample {

	public static void main(String[] args) 
		int a=10;
		System.out.println("Before Change value of a: "+a);
		// Passing value as a parameter. OR
		// Call method by value.
		// Original value of variable ‘a’ doesn’t get changed.
		System.out.println("After Change value of a: "+a);
	public static void changeValue(int x)
		System.out.println("Value of x: "+x);


Before Change value of a: 10
Value of x: 30
After Change value of a: 10

Call by Reference or Passing Reference via Method

       This can be done only using the Objects. As the name depicts we have to pass a reference(address) of the objects. Actually, it works same like pointers . We pass the reference via a method which points to a variable(s), now this variable can be handled by calling method.

Call by value


public class callByRef 
	int testData=50;
	public static void main(String[] args) 
		// Creating object of Class
		callByRef objTemp=new callByRef();
		System.out.println("Before calling method: "+objTemp.testData);
		// Passing object reference in Method parameter. OR
		// Calling method by reference.
		System.out.println("After calling method: "+objTemp.testData);
		System.out.println("After increasing value: "+objTemp.testData);
	public static void increaseValue(callByRef obj)
		// Value get increased in the same variable.

Before calling method: 50
After calling method: 150
After increasing value: 175

Example -2

public class CallByRef_2 {

	public static void main(String[] args) 
		StringBuffer sb=new StringBuffer("JAI ");
		System.out.println("Before change: "+sb);
		System.out.println("After change: "+sb);
		sb.append(" BHARAT");
		System.out.println("After more change: "+sb);

	public static void changeValue(StringBuffer obj)


Before change: JAI 
After change: JAI HIND
After more change: JAI HIND BHARAT

2800total visits,3visits today

Leave a Reply

Your email address will not be published. Required fields are marked *