variable might already have been assigned when it cannot be assigned

后端 未结 4 2124
梦如初夏
梦如初夏 2021-01-17 19:31

research this code:

public class TestFinalAndCatch {
    private final int i;

    TestFinalAndCatch(String[] args) {
        try {
            i = method1()         


        
4条回答
  •  感动是毒
    2021-01-17 20:01

    i is final, so it can only be assigned once. The compiler is probably not smart enough to realize that if an exception is thrown, the first assignment won't take place, and if an exception isn't thrown, the second assignment won't take place.

提交回复
热议问题