5 ways to Return multiple values from a method in Java

In this post, we will see how to return multiple values from a method in Java.


As per 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


Name is Ryan
Age is 25
Gender is M

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

Below is simpler version of 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


Returned Values are [Ryan, 25, M, 100000]
Collection size is : 4
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. javafx.util.Pair class in Java 8

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




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


3. Return an Array of Specific Type or an Object Array

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


Download   Run Code


[30, 50, 100]

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

Download   Run Code


[Ryan, 25, M, 100000]


4. Return a Collection

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

Download   Run Code


[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


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


5. Using a delimiter

The idea here 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



Also see:

1. Return multiple values from a method in C

2. How to return multiple values from a function in C++

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

newest oldest most voted
Notify of

Can you please tell me why Arrays.toString(ints) is used in return an array of specific type example and why is it not used in return a collection example?

Answer would be much appreciated

Adam Siemion

You did not mention Guava’s Table.