Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:15:07+00:00 2026-06-03T20:15:07+00:00

Preamble: I am aware of using a list or other collections to return a

  • 0

Preamble: I am aware of using a list or other collections to return a result but then I have to go through the list plucking the results out: see 2nd example

Preamble-2: I’m looking for an answer beyond “this is not supported in Java …”


I’m looking for a convenient way to return multiple objects from a Java method call.

Kind of like in PHP:

list ($obj1, $obj2, ...) foobar();

I’m really getting tired of passing holder objects in the arguments for example:

class Holder {
   int value;
}

Holder h1=new Holder();
Holder h2=new Holder();

and then:

o.foobar(h1,h2);

… would be very interested if someone has figured an elegant way to get round this.


Using a list

List<String> = foobar();

There are two drawbacks to this:

I have to first pack the List on the callee side of the house:

// this is on the callee side of the house
ArrayList<String> result = new ArrayList<String>
result.add("foo");
result.add("bar");

Then on the caller side I have to pluck the results out:

// This is on the caller side
List<String> result = foobar();
String result1 = result.get(0);
String result2 = result.get(1); // this is not as elegant as the PHP equivalent

Further, say I wanted to return objects of different types say String, Integer I would have to return a list of Objects and then cast each object … not pretty

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:15:08+00:00Added an answer on June 3, 2026 at 8:15 pm

    Using a generic tuple class is the closest I can imagine. To give an idea, such general purpose class for three return values could look something like this:

    public class T3<A, B, C> {
        A _1;
        B _2;
        C _3;
        T3(A _1, B _2, C _3) {
            this._1 = _1;
            this._2 = _2;
            this._3 = _3;
        }
    }
    

    This allows a one statement constructor on the callee side, for example:

    public T3<String, Integer, Character> getValues() {
        return new T3<String, Integer, Character>("string", 0, 'c');
    }
    

    but no holder instance construction by the callee is to be done.

    Each number of return values needs a tuple-class of its own. There might be something like that provided in the Functional Java library. For reference, here’s an N-Tuple implementation.

    For the two-valued case java.util.AbstractMap.SimpleEntry would do:

    return new SimpleEntry<String, Integer>("string", 0);
    

    Anyhow, I see two important points to consider for multiple return values:

    1. Typed return values
    2. Pointing out the meaning of each return value

    As boring as it is, I’d recommend creating a separate class to hold all the response values and declare that as the method return type. That way the meaning of each value can be highlighted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked through many tutorials, as well as other question here on stack
Preamble Using VTK library with C++, quite often I have to write something like
A preamble: I've been using PHP for over 8 year every now and then,
Preamble To build dynamic web-sites, we have to master at least four languages: HTML
Small preamble. I was good java developer on 1.4 jdk. After it I have
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
The preamble: I have designed a strongly interfaced and fully mockable data layer class
Without any preamble I want to show you problem I have in my program,
I am using ntheorem to typeset a set of conditions. In my preamble I
I have the following YAML: - name: List of monkeys - author: Nicolas Raoul

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.