Java Glossary : return

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : R words : return.

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 logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1023.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/return.html J:\mindprod\jgloss\return.html