Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The key here is that you are returning a reference value to the respective object from your method. And in the caller side, what you get is the copy of the reference of the method. Remember, everything in Java are passed by value. Even the references are passed by value.</p> <p>When you modify the object using the reference in the <code>finally</code> block, the changes will be visible on the caller side. But, if you change the value of the <code>reference</code> itself, then the changes would of course not be reflected on the caller side, as now both of them have a different references.</p> <blockquote> <p>Before this method returns, the value of productName is modified in the finally block.</p> </blockquote> <p>What you have done in the <code>finally</code> block is, you've assigned a new <code>String</code> object to the <code>productName</code> reference. That will change the the reference value of <code>productName</code>. And thus as per the above paragraph, the changes wouldn't be reflected on the caller end. Both of them are dealing with 2 different String objects.</p> <blockquote> <p>Therefore, the method should return this new modified value.</p> </blockquote> <p>No, it shouldn't. Because both the String objects are different alltogether. Try this code with a <code>StringBuilder</code> object:</p> <pre><code>public StringBuilder getProductName() { StringBuilder sb = new StringBuilder(productName); try { return sb; } finally { sb.append("Modified"); } } </code></pre> <p>Now the changes to the <code>StringBuilder</code> will be reflected in the return value. So what is happening here?</p> <p>First with the following return statement:</p> <pre><code>return sb; </code></pre> <p>you returned a reference to the <code>StringBuilder</code> object you created. Then in the <code>finally</code> block:</p> <pre><code>sb.append("Modified"); </code></pre> <p>.. you've modified the object pointed to by <code>sb</code>. Since you haven't changed the reference value of <code>sb</code> itself, the changes to the object will be visible for all the references pointing to that object, and hence the one at the caller end too.</p> <p>Now modify the <code>finally</code> block as:</p> <pre><code>sb = new StringBuilder("Modified"); </code></pre> <p>and see whether the change is reflected or not. You will not see any change in return value. Because, now you have changed the reference value of <code>sb</code> itself. </p> <p>Further, if you return the modified reference from the <code>finally</code> block, the original returned value would be overwritten. Try modifying your <code>finally</code> block to:</p> <pre><code>finally { this.productName="The product name has been modified in the finally block."; System.out.println("Product name in the finally block : "+this.productName); return this.productName; // Note: This is bad idea } </code></pre> <p>Now you will see the new value.</p> <hr /> <p><strong>See also:</strong></p> <ul> <li><a href="https://stackoverflow.com/q/18131447/1679863">java: try finally blocks execution</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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