As a rule, the finally block is always executed whether or not an exception is thrown in the try block or a continue, break or return statement encounters in the try block itself.
Accordingly, the return value of the method in the code snippet given below should be changed but it doesn't.
final class Product
{
    private String productName=null;
    public Product(String productName)
    {
        this.productName=productName;
    }
    public String getProductName()
    {
        try
        {
            return this.productName;
        }
        finally
        {
            this.productName="The product name has been modified in the finally block.";
            System.out.println("Product name in the finally block : "+this.productName);
        }
    }
}
public final class Test
{
    public static void main(String...args)
    {
        System.out.println(new Product("Intel core i3").getProductName());
    }
}
The method getProductName() simply returns the product name which is assigned by the constructor to the field productName before this method is invoked.
Before this method returns, the value of productName is modified in the finally block. Therefore, the method should return this new modified value but it returns the initial value which is assigned to productName when an instance of this class has been constructed.
It produces the following output:
Product name in the finally block : The product name has been modified in the finally block.
Intel core i3
Why doesn't this method return the new value which is assigned to the productName field in the finally block? It should return the new value. Shouldn't it?
 
     
     
     
    