user818700
user818700

Reputation:

How to let method return more than one object?

This is probably a very stupid question. But is there a way that a method could return more than one object? Like say, 2 Strings?

public static String multipleReturnMethod(){

   String a = "a";
   String b = "b";

   return a;
   return b;
}

Possible? Not Possible? How do you make it possible?

Upvotes: 1

Views: 2288

Answers (8)

LordDoskias
LordDoskias

Reputation: 3191

One way is to pass an output object to your method, for example in the form of an arraylist, so it will be something like:

public static ArrayList<String> multipleReturnMethod(ArrayList<String> output) {

 String a = "a";
 String b = "b";

 output.add(a);
 output.add(b);
 return output; //not really necessary
}

Of course output has to be created outside your method.

Upvotes: -1

ChadNC
ChadNC

Reputation: 2503

If they are all the same type of object then the simplest way is to just return an array of that type of object

public static String[] methodA(){
  String[] temp = new String[5];
  // do work
  return temp;
}

If you wish to return different types of objects then return an ArrayList of objects.

public static ArrayList<Object> methodB(){
  ArrayList<Object> temp = new ArrayList<Object>();
  // do work
  return temp;
}

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533550

If you have a more complex example, you can use a listener pattern

interface ResultListener {
   void resultOne(String text);
   void resultTwo(String text, int number);
}

public static void multipleReturnMethod(ResultListener result){
   result.resultOne("a");
   result.resultOne("b");
   result.resultTwo("C", 1);
}

As you can see you can return any combination of results to the caller with a mix or combination of types.

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114787

If you want to keep the signature, you could concatenate the strings with a defined delimiter:

public static final String DELIMITER = "\t";  // or something else 
                                              // not used in strings

public static String multipleReturnMethod(){

   String a = "a";
   String b = "b";

   return a + DELIMITER + b;
}

(although I prefer changeing the signature and returning an array or a collection of values, see Jon's answer)

Upvotes: 1

peko
peko

Reputation: 11335

It's impossible to return more then one object, never!. Solution is to return an object wich contains the objects you want to return.

Upvotes: 0

John B
John B

Reputation: 32959

It is not possible in Java. You must create a wrapper class around the elements you want to return and return that single element and contains everything you want to return.

FunctionalJava has a handy class P for Product that makes this easy. http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/P.html

return P.p(a,b);

Upvotes: 0

dacwe
dacwe

Reputation: 43504

Not possible without chaning the return object:

  1. Either create a class for it:

    class Strings {
        String a, b;
        Strings(String a, String b) {
            this.a = a;
            this.b = b;
        }
    }
    
    public static Strings multipleReturnMethod(){
         return new Strings("a", "b");
    }
    
  2. Or return an array

    public static String[] multipleReturnMethod(){
        return new String[] { a, b };
    }
    

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500983

You could return an array...

public static String[] multipleReturnMethod(){    
   String a = "a";
   String b = "b";
   return new String[] { a, b };
}

Or a list, or a type which encapsulates the two values, etc...

You can only return a single value from a method, but that value may itself give access to multiple "subvalues".

If you can give more information about what you're trying to do, we may be able to help more.

Upvotes: 6

Related Questions