return : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (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
    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.

This page is posted
on the web at:

http://mindprod.com/jgloss/return.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\return.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.15.10.104]
You are visitor number