Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot truly return multiple values, but you <strong>can</strong> pass objects into a method and have the method mutate those values. That is perfectly legal. Note that you cannot pass an object in and have the object itself become a different object. That is:</p> <pre><code>private void myFunc(Object a) { a = new Object(); } </code></pre> <p>will result in <em>temporarily</em> and <em>locally</em> changing the value of <code>a</code>, but this will not change the value of the caller, for example, from:</p> <pre><code>Object test = new Object(); myFunc(test); </code></pre> <p>After myFunc returns, you will have the <em>old</em> Object and not the new one.</p> <p>Legal (and often discouraged) is something like this:</p> <pre><code>private void changeDate(final Date date) { date.setTime(1234567890L); } </code></pre> <p>I picked <code>Date</code> for a reason. This is a class that people widely agree should never have been mutable. The the method above <em>will</em> change the internal value of any <code>Date</code> object that you pass to it. This kind of code is legal when it is very clear that the method will mutate or configure or modify what is being passed in.</p> <p>NOTE: Generally, it's said that a method should do one these things:</p> <ul> <li>Return void and mutate its incoming objects (like <code>Collections.sort()</code>), or</li> <li>Return some computation and don't mutate incoming objects at all (like <code>Collections.min()</code>), or</li> <li>Return a "view" of the incoming object but do not modify the incoming object (like <code>Collections.checkedList()</code> or <code>Collections.singleton()</code>)</li> <li>Mutate one incoming object and return it (<code>Collections</code> doesn't have an example, but <code>StringBuilder.append()</code> is a good example).</li> </ul> <p>Methods that mutate incoming objects <em>and</em> return a separate return value are often doing too many things.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload