109

Is it possible to return two or more values from a method to main in Java? If so, how it is possible and if not how can we do?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
user1089660
  • 1,191
  • 2
  • 7
  • 6
  • 2
    Return array, list, set, map or your custom object containing multiple values. I have seen this same question somewhere.... let me find that. There are multiple questions on this topic: http://stackoverflow.com/search?q=return+multiple+values+in+java%3F – Harry Joy Dec 19 '11 at 06:10
  • Maybe too late, but Just use java.util.AbstractMap.SimpleIEntry. Reusable, everywher since 1.6 – yerlilbilgin Feb 04 '20 at 07:47
  • If you are on Java 14+, it is better in general to use a Java Record instead of a SimpleEntry for representing a pair of values. – Jason Jan 30 '21 at 14:52

3 Answers3

91

You can return an object of a Class in Java.

If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that class.

If you want to return unrelated values, then you can use Java's built-in container classes like Map, List, Set etc. Check the java.util package's JavaDoc for more details.

biniam
  • 8,099
  • 9
  • 49
  • 58
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • I would add one more point to this - keep your returned object immutable (best initialize it in a constructor and keep your fields final) – Kris Dec 19 '11 at 13:35
  • 4
    You can return an object , but if you have so many return objects in your program it makes the source code complicated. for things that are returned from a method once-of-its-kind-it-the-program better to have Tuples. like c#. darling c# its a great language – EKanadily Aug 22 '15 at 22:41
  • 1
    I guess there should be C# `out` or `ref` parameter equivalent in Java, otherwise I will need to create class even for returning two variables and for each function returning multiple values. If we follow this, then there will be a class for every function returning multiple values. Though using `Map` feels better approach now. – Mahesha999 Sep 18 '15 at 08:52
  • I would use a map or set for keeping it simple :) – Saqib Javed Sep 21 '17 at 08:10
  • I think you mean `If you are returning more than 1 value that are un-related` and vice versa – User3 Nov 24 '17 at 10:12
  • 3
    Returning maps, lists or sets (to avoid returning a DTO) IMHO is an antipattern. – Lluis Martinez Jan 14 '19 at 17:18
41

You can do something like this:

public class Example
{
    public String name;
    public String location;

    public String[] getExample()
    {
        String ar[] = new String[2];
        ar[0]= name;
        ar[1] =  location;
        return ar; //returning two values at once
    }
}
yshavit
  • 42,327
  • 7
  • 87
  • 124
RanRag
  • 48,359
  • 38
  • 114
  • 167
38

You can only return one value, but it can be an object that has multiple fields - ie a "value object". Eg

public class MyResult {
    int returnCode;
    String errorMessage;
    // etc
}

public MyResult someMethod() {
    // impl here
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722