Java Call By Value


When a method is called by value, a copy of the original argument is passed to the called method. The copied argument occupy separate memory location than the actual argument. If any changes were done to values inside the method, it is only visible inside the method. Their values remain unchanged outside it.


Example 1:-

In this example call by value original value does not changed.

public class Callbyvalue
{  
	int amt=60;  
	void interest(int amt){  
		amt=amt+100; 
	}  
	public static void main(String args[])
    {  
		Callbyvalue cv=new Callbyvalue();  
		System.out.println("Before Interest Amount "+cv.amt);  
		cv.interest(250);// call by value  
		System.out.println("After Interest Amount "+cv.amt);  
	}  
} 

Example 2:-

In this example we are passing object as a value.

public class Callbyvalue1
{  
	int amt=60;  
	void interest(Callbyvalue1 cv){  
		cv.amt=cv.amt+100; 
	}  
	public static void main(String args[])
    {  
		Callbyvalue1 cv=new Callbyvalue1();  
		System.out.println("Before Interest Amount "+cv.amt);  
		cv.interest(cv);// passing object as a value  
		System.out.println("After Interest Amount "+cv.amt);  
	}  
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.