This post will discuss how to return multiple values from a method in Java.

As per the Java Language Specification, the methods in Java can return only one value at a time. So returning multiple values from a method is theoretically not possible in Java. But the beauty of Java lies in the fact that we can do desired things with some smart workarounds. This post provides an overview of some of the available alternatives to accomplish this.

1. Using a POJO class instance

This is the most commonly used method to return multiple values from a method in Java. The idea is to return an instance of a class containing all fields we want to return.

This class can be a POJO with public member variables and a public constructor to initiate its fields with required values or a JavaBean with corresponding getters and setters for their private member variables and no-arg public constructor.

Download  Run Code

Output:

Name is Ryan
Age is 25
Gender is M

 
The benefits of using this method over other methods discussed below are that it provides the type-safety and considerably improves the code’s readability.

Following is a simpler version of the above program that uses an object array to store all the required fields, but it doesn’t offer any type-safety or even pass field information to the caller.

Download  Run Code

Output:

The returned values are [Ryan, 25, M, 100000]
The collection size is 4
The name field has value Ryan

 
We can also implement a generic Pair or generic Tuple, which also offers type-safety if we just need to return two or three fields from the method.

2. Using javafx.util.Pair class

From Java 8 onward, we can use the Pair class included in the javafx.util package representing the name-value pairs.

Download Code

Output:

{name : Ryan}
{age : 25}
{gender : M}

3. Return an array of specific type or an object array

This is the simplest approach so far but doesn’t offer type-safety (for an object array) or pass field information to the caller. For arguments of the same type, we can do something like:

Download  Run Code

Output:

[30, 50, 100]

 
For multi-type arguments, we can return an array of Objects, as shown below:

Download  Run Code

Output:

[Ryan, 25, M, 100000]

4. Return a Collection

Instead of returning an array, we can also return a Collection. For example, the following program uses a List instead of an array.

Download  Run Code

Output:

[Ryan, 25, M, 100000]

 
Using a List doesn’t pass field information to the caller. We can create a map instead to pass the field name and its value as key-value pairs, as shown below:

Download  Run Code

Output:

income is 100000$
gender is Male
name is Ryan
age is 25

5. Using a delimiter

Here, the idea is to return a string consisting of all values separated by a delimiter. We then need to split the string using the same delimiter before using the values inside the caller method.

This approach is not so neat but still vastly used in many legacy projects to transfer data between model and view.

Download  Run Code

Output:

George
Martin

That’s all about returning multiple values from a method in Java.

 
Also see:

Return multiple values from a function in C

Return multiple values from functions in C++