Could a final variable be reassigned in catch, even if assignment is last operation in try?

前端 未结 12 1162
粉色の甜心
粉色の甜心 2020-12-04 18:49

I am quite convinced that here

final int i;
try { i = calculateIndex(); }
catch (Exception e) { i = 1; }

i cannot possibly have

12条回答
  •  执念已碎
    2020-12-04 19:52

    But i may be assigned twice

        int i;
        try {
            i = calculateIndex();  // suppose func returns true
            System.out.println("i=" + i);
            throw new IOException();
        } catch (IOException e) {
            i = 1;
            System.out.println("i=" + i);
        }
    

    output

    i=0
    i=1
    

    and it means it cannot be final

提交回复
热议问题