1

Why in the below code,the value of x is not updating although the finally block executes,

public class Student
{
    public static void main(String...args)
    {
        ABC obj=new ABC();
        System.out.println("x : "+obj.display());
    }
}

class ABC
{
    int x;
    int display()
    {
        try
        {
            x=10;
            System.out.print("Inside Try ");
            return x;
        }
        finally
        {
            x=x+20;
            System.out.print("I execute ");
        }
    }
}

The output of this code is

Inside Try I execute x : 10

Although the finally block is executing, the value of x is not changing. Can somebody please explain why?

user207421
  • 305,947
  • 44
  • 307
  • 483
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42

0 Answers0