return : Java Glossary
home R words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 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)
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
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 22,494. 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/return.html J:\mindprod\jgloss\return.html