News

Tutorial: How to Return Object from a Method in JAVA

return_object

First we will discuss return type of any method. Then we will see the control flow from invoking code to invoked code and again when control goes back to invoking code. After learning about method overloading and constructor overloading in Java, the content of Java object methods will be easier.

A method returns to the code that invoked it when it:

  1. completes all the statements in the method,
  2. reaches a return statement, or
  3. throws an exception (covered later)

Whichever occurs first between the last two.

You declare a method’s return type in its method declaration. Within the body of the method, you use the return statement to return the value.

Any method declared void doesn’t return a value. Meanwhile it does not require to contain a return statement, but if you wish to put you can.  In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:

return;

If you try to return a value from a method that is declared void, you will get a compiler error.

Any method that is not declared void must contain a return statement with a corresponding return value, like this:

return returnValue;

So far it was a recapitulation what we know earlier.

Now we will learn how to return object after a method call. It seems to be strange. But its true that A method can return any type of data, including class types that you create.

We will understand how it happens with the following example given below.

Program

class Employee{
  double salary;
  Employee(double salary){
    this.salary = salary;
  }
  Employee updateSalary(double salary){
    Employee employee = new Employee(this.salary+salary);
    return employee;
  }
  double getSalary(){
    return this.salary;
  }
}
class ReturnObjectDemo{
  public static void main(String args[]){
    Employee kallis = new Employee(34029.48);
    Employee ronaldo;
    ronaldo=kallis.updateSalary(6295.28);
    System.out.println("Salary of Kallis is: "+kallis.getSalary());
    System.out.println("Salary of Ronaldo is: "+ronaldo.getSalary());
  }
}

Output

return_object

Explanation of the Java Code & Output

As you can see, each time updateSalary( ) is invoked, a new object is created, and a reference to it is returned to the calling routine. The preceding program makes another important point: Since all objects are dynamically allocated using new, you don’t need to worry about an object going out-of-scope because the method in which it was created terminates. The object will continue to exist as long as there is a reference to it somewhere in your program. When there are no references to it, the object will be reclaimed the next time garbage collection takes place.

Here object ronaldo of class type Employee is created when updateSalary() is being invoked by the object kallis of Employee class. But the point that is to be noticed that is both even though ronaldo is created from object kallis but both will have separate copies of instance variables.

Checkout more useful tutorials and definitive guidelines on Java programming here.

Click to comment

Leave a Reply

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

To Top