return : Java Glossary

go to home page R words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 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) ©1996-2009 Roedy Green, Canadian Mind Products
return
There are three general ways to get information computed inside a method back to the caller, and one extra way that only works if caller and callee are in the same class:
  1. You can return one and only one value to the caller (primitive or object) with return. Java allows multiple inputs to a method but only one output. This restriction is very deeply burned into both the design of Java and the JVM, e. g.
    return a+b;
  2. Hide the values you want to return inside objects that were passed as parameters. e.g.
    void myMethod( Thing t )
       {
       t.a = 42;
       return;
       }
    // end myMethod
  3. hide them inside an object (possibly an array) created inside the caller, and return that object, e. g.
    int[] myMethod()
       {
       // ...
       int[] r = new int[3];
       r[0] = ans0;
       r[1] = ans1;
       r[2] = ans3;
       return r;
       }
    // end myMethod
  4. If caller and callee belong to the same class, there is another technique which must be used with care. Hide the information in instance or static variables that both caller and callee can access. There is a way around this same class restriction, but it would generally be considered poor programming form, so I won’t corrupt you with the details.
The one thing a method cannot do is make the caller’s parameter variables change value, or point to different objects, the way you can in Pascal or C++. If you change a parameter’s value inside a called method, you only change the callee’s local copies.

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/return.html J:\mindprod\jgloss\return.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.102]
You are visitor number 42,917.