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

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

A pointer is a variable which contains the address of another variable. Or we can say that a pointer is a variable whose value is the address of another value.

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.