This developer guide tells how to Return Object from a Method in JAVA using Java Object Methods. First, we will discuss the 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.
How to Return Object from a Method in JAVA
A method returns to the code that invoked it when it:
- Completes all the statements in the method
- Reaches a return statement
- or Throws an exception (covered later)
Whichever occurs first between the last two. Make sure to declare a method’s return type in its method declaration. You can use the return statement to return the value within the body of the method.
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 that we know earlier.
How to return object after a method call in Java
Now we will learn how to return an object after a method call. It seems to be strange. But it’s 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
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 even though ronaldo is created from object kallis but both will have separate copies of instance variables.
If you are looking for help with java programming homework, Check out more useful tutorials and definitive guidelines on Java programming here.