Return Object as a function return type in Java Program

How to return object value in java program?

– in many case we need multiple value in return statement that time we can use object as return type and solve this problem. It is called return object.

In this blog program we find maximum total income of one company’s two years’ total income record and display maximum total income of year data.

 

 

Let’s see example on Return object.

class Company

{

            double totalInc;

            double profit,inv;

 

            void setData(double totalInc,double inv)

            {

                        this.totalInc  = totalInc;

                        this.inv = inv;

                        this.profit = this.totalInc – this.inv;

            }

            void display()

            {

                        System.out.println(“Total Income : ” + this.totalInc);

                        System.out.println(“Inverstment : ” + this.inv);

                        System.out.println(“Profit : ” + this.profit);

            }

            // Just Change Return Type Of this Method

            Company TotalProfitOfTwoYear(Company obj1,Company obj2)

            {

                        // Create one TEMP Object

                        Company temp = new Company();

 

                        if(obj1.totalInc > obj2.totalInc)

                        {

                                    temp = obj1;     // Store 2014 Record in temp object

                        }

                        else

                        {

                                    temp = obj2;     // Store 2015 Record in temp object

                        }

 

                        // Return temp object

                        return temp;

            }

}

class clEx8

{

            public static void main(String args[])

            {

                        Company obj2014 = new Company();

                        Company obj2015 = new Company();

 

                        obj2014.setData(1200000,400000);

                        obj2015.setData(1500000,450000);

 

                        System.out.println(“=== Record Of 2014 ===”);

                        obj2014.display();

                        System.out.println(“=== Record Of 2015 ===”);

                        obj2015.display();

 

 

                        Company TotalTemp = new Company();

                        TotalTemp = TotalTemp.TotalProfitOfTwoYear(obj2014,obj2015);     // Pass And Return the object using methods

                        // TotalTemp has three record : totalInc , inv and profit

 

                        System.out.println(“=== Maximum Record ===”);

                        TotalTemp.display();    // Display Maximum Record Data

            }

}

 

Output:

=== Record of 2014 ===

Total Income : 1200000

Investment : 400000

Profit : 800000

=== Record of 2015 ===

Total Income : 1500000

Investment : 450000

Profit : 1050000

Profit of Two Year – 1850000

=== Maximum Record ===

Total Income : 1500000

Investment : 450000

Profit : 1050000

Profit of Two Year – 1850000

 

In this blog, we can see year 2015 has maximum total income that 2014. For that we include temp object and store maximum record data in it, then value return into main program.

 

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

218total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint