Take the 2-minute tour ×

I have one Main class and VOCollection Class. in main class there is a method called getStatus(), from this method only i am getting some status(true,false), if the status is true, i need to return a collection. at present i have two ideas, but both are expensive.

  1. return map, it's expensive because setting Boolean for collection make confusion in code, and only one Boolean value is enough (but we are returning multiple).

  2. creating an instance variable in VOCollection class, and having getter & setter to get & set the Boolean value. this is also expensive. (creating a variable in another class).

give me less expensive solution.

share|improve this question
Your question is very unclear. Show some example code for each option. –  Jim Garrison May 31 '11 at 2:44

6 Answers 6

up vote 5 down vote accepted

There are a number of ways to do this:

  • Return null to indicate that there is nothing present like java.util.Map.get()
  • Create a custom class to return both parameters. (See other answer)
  • Use a 1 element array for one of the return values. boolean method(List[] result) { result[0] = answer; return flag; }
  • Use a library like Google Guava that has a Pair/Tuple class: Pair method() { return new Pair(flag, answer); }
  • Change your code so that it isn't necesssary, this is usually the right answer. See the exception comment or change the way things are passed around.
share|improve this answer
you are right. last point is correct in my case :) –  Chandra Sekhar May 31 '11 at 12:17

You can introduce an class like this

class StatusResult
    public final bool status;
    public final VOCollection result;


and change the signature of getStatus() to

StatusResult getStatus()
share|improve this answer
If I'm reading your question correctly, this is what you want. It would be nice if Java supported tuples for cases like this. –  Dan May 31 '11 at 2:52
Yes You are right, but i don't need new class to store this. –  Chandra Sekhar May 31 '11 at 12:16

The desire to return either an object or a boolean is smelly. You see this kind of "flag" very often in code of starters who don't fully understand how to handle exceptional or "nothing" outcomes.

If the boolean represents an exception, just let it go or rethrow it in another exception. E.g. IllegalArgumentException, IllegalStateException, UnsupportedOperationException, etc depending on the functional requirement. You can put the calling code in a try-catch and handle it accordingly.

Or if the boolean represents a state of "nothing", just return null or an empty collection. You can handle it by testing the return value after calling the method.

share|improve this answer

I have come across the same situation in my past.

Both the solutions which you have mentioned are feasible as you said they may be expensive.

In fact, Java doesnot support the multiple return values from a method.

Solution Which I implemented was

I will try to parse/create/encode the string in such a way, after decoding i should be able to know what i need to do.

In your case, I would have choosen the following path Create a string like returnValue, let that string start with either 0 or 1, if its 1 its true, else false. and append the second return value to that.

For example you are trying to return like "returnValue1" and "true" for true let us keep it 1 and for false lets keep it 2 when you do this, your return value will become "1returnValue1"

share|improve this answer

Simply return the collection when 'status is true'. Otherwise return null.

But: Getting a collection with calling a method named "getStatus()" is not what I would expect.

"getStatus()" sounds getting some constant value indicating internal state. (like enum constant, int status code whatever)

In your scenario I would recommend having one method returning a boolean indicating that "i have something or not" and another method returning the collection - that what you have or null when not. The boolean-method can then be easy implemented by calling the collection-method checking != null.

public boolean isXyz() {
    return getXyz() != null;

public Collection getXyz() {
    return yourCollection; //maybe null
share|improve this answer

The simplest approach may be to always return a VOCollection. For the situation where there is no data you could return a collection with no data i.e. isEmpty() is true, or a collection which has a flag (but I prefer the first option).

To avoid creating an object each time, create a single instance of an immutable VOCollection with no entries. This is what you can return when you have no data.

share|improve this answer

Your Answer


