2 return values from a method


General J2EE: 2 return values from a method

  1. 2 return values from a method (3 messages)

    What...if any Is the preferred way to return more than one (lets just say 2) values from method? Use an Array?

    Threaded Messages (3)

  2. 2 return values from a method[ Go to top ]

    For a one off, you can use an array. However, most of the time if I need to return multiple values I create a class which holds those "N" values.

  3. ok[ Go to top ]

    We ended up creating a "Util" class. The one-off was the easier way to do things, but i guess not the best in terms of OO. thanks.
  4. pair class[ Go to top ]

    I created a number of pair classes (immutable, thread-safe, etc.) which make this very easy.

    class Pair {
      Object first;
      Object second;
      Pair(Object f, Object s) { first = f; second = s; }

    You would also override toString(), equals() and hashCode().

    public Pair getValues() { return new Pair(value1, value2); }

    Works great!

    Hope this helps!