multiple return values : Java Glossary
home M words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-04-04 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
multiple return values
You can pass multiple parameters to a method, how do you get multiple values back? Here are four methods:
  1. The official way is to invent a new class with fields for each value with getters and setters for each field. With a decent IDE like IntelliJ Idea, this is fairly painless. You return an object of this new class from your method. Sun uses this technique with Point, Dimension and Location. If you can’t think of a good inclusive English name for this new class, then these two values are not logically related and you should not be writing a method to return multiple fields at once.
  2. the quick and dirty way, you return an array of heterogeneous objects e.g.
    // kludge to return multiple objects from a method.
    
    return new Object[] { aString, new Integer( anInt ) };
    
    // The caller then has to unpack.
    
    Object[] junk = aMethod();
    
    String aString = (String) junk[0];
    
    int anInt = ((Integer) junk[1]).intValue();
    The unboxing intValue() is automatic in JDK 1.5+.

    The two main reasons not to use this technique are:

    • You defeat compile-time type consistency checking. If you use String[] (or similar specific type) instead of Object[] the technique is not quite as disreputable. You might even pass in multiple arrays of different types, but that is getting ugly.
    • It is not at all clear from the code which data goes in which slot. Maintenance programmers will tend to change the assignment and fail to keep the documentation up to date. The code is thus almost as unreadable as machine language.
  3. Pass in an Object as a parameter, and have the callee modify that Object’s fields. You will see Sun doing this with Dimension objects, for example.
  4. Split the work up into several method calls. The first does the computation and the rest retrieve but one of the results each. You can eliminate the computation call by having the result-retriever methods check if the computation is up to date, using lazy evaluation of the results. This is how Sun’s GregorianCalendar works.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 3,571. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/multiplereturn.html J:\mindprod\jgloss\multiplereturn.html