Create a class, CallClass, with two methods to show call by value and call by reference. One method has two parameters that are any primitive types, whereas the other has a parameter that is an object of any class. Create a CallClassTest class that calls the two methods with proper arguments to show call by value and call by reference. Java
Create a class, CallClass, with two methods to show call by value and call by reference.
One method has two parameters that are any primitive types, whereas the other has a
parameter that is an object of any class. Create a CallClassTest class that calls the two
methods with proper arguments to show call by value and call by reference.
Java
Java Code: Java code that has a class which have two methods. One method takes call by value and other method takes an object of class. Screenshot of the code and output has been attached.
import java.io.*;
class CallClass//class CallClass
{
int e,f;//data member of the class
public CallClass()//constructor
{
e = 15;//initialization of members
f = 20;
}
public void callVal(int a,int b)//method that takes parameters as value
{
System.out.println("Call by Value a: "+a);//print values of parameters passing
System.out.println("Call by Value b: "+b);
}
public void callRef(CallClass c)//method takes argument as CallClass object
{
System.out.println("Call by Reference a: "+c.e);//by class reference access data members
System.out.println("Call by Reference b: "+c.f);
}
}
class Main//test class
{
public static void main(String[]args)
{
CallClass c1 = new CallClass();//creates object of the class
c1.callVal(5,10);//call by value
c1.callRef(c1);//call by Reference
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images