Passing and Returning Objects in Java

This is a Java Program to Implement the Concept of Passing the Object and Return Object.

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.

  1. import java.util.Scanner;
  2. public class Object_Pass_Return 
  3. {
  4.     int length, breadth, area;
  5.     Object_Pass_Return area1(Object_Pass_Return object1)
  6.     {
  7.         object1 = new Object_Pass_Return();
  8.         object1.length = this.length;
  9.         object1.breadth = this.breadth;
  10.         object1.area = object1.length * object1.breadth;
  11.         return object1;
  12.     }
  13.     Object_Pass_Return area2(Object_Pass_Return object2)
  14.     {
  15.         object2 = new Object_Pass_Return();
  16.         object2.length = this.length + 5;
  17.         object2.breadth = this.breadth + 6;
  18.         object2.area = object2.length * object2.breadth;
  19.         return object2;
  20.     }
  21.     public static void main(String[] args) 
  22.     {
  23.          Object_Pass_Return obj = new Object_Pass_Return();
  24.          Scanner s = new Scanner(System.in);
  25.          System.out.print("Enter length:");
  26.          obj.length = s.nextInt();
  27.          System.out.print("Enter breadth:");
  28.          obj.breadth = s.nextInt(); 
  29.          Object_Pass_Return a = obj.area1(obj);
  30.          Object_Pass_Return b = obj.area2(obj);
  31.          System.out.println("Area:"+a.area);
  32.          System.out.println("Area:"+b.area);
  33.     }
  34. }

Output:

$ 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.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.