Enter length and breadth of rectangle as input. After that we create object of that class and pass the reference variable to two different methods. Now we calculate area for two different objects and return them to main method. Hence we get two different areas as output.
Here is the source code of the Java Program to Implement the Concept of Passing the Object and Return Object. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Object_Pass_Return
int length, breadth, area;
Object_Pass_Return area1(Object_Pass_Return object1)
object1 = new Object_Pass_Return();
object1.length = this.length;
object1.breadth = this.breadth;
object1.area = object1.length * object1.breadth;
Object_Pass_Return area2(Object_Pass_Return object2)
object2 = new Object_Pass_Return();
object2.length = this.length + 5;
object2.breadth = this.breadth + 6;
object2.area = object2.length * object2.breadth;
public static void main(String args)
Object_Pass_Return obj = new Object_Pass_Return();
Scanner s = new Scanner(System.in);
obj.length = s.nextInt();
obj.breadth = s.nextInt();
Object_Pass_Return a = obj.area1(obj);
Object_Pass_Return b = obj.area2(obj);
$ javac Object_Pass_Return.java $ java Object_Pass_Return Enter length:5 Enter breadth:6 Area:30 Area:120
Sanfoundry Global Education & Learning Series – 1000 Java Programs.