Problems with local variable scope. How to solve it?

后端 未结 4 1754
生来不讨喜
生来不讨喜 2020-11-28 09:23

I\'m getting the following error when trying to execute statemet.executeUpdate() in my code:

Local variable statement defined in an enclosing sc         


        
4条回答
  •  心在旅途
    2020-11-28 09:47

    not Error:

    JSONObject json1 = getJsonX();

    Error:

    JSONObject json2 = null;
    if(x == y)
       json2 = getJSONX();
    

    Error: Local variable statement defined in an enclosing scope must be final or effectively final.

    But you can write:

    JSONObject json2 = (x == y) ? json2 = getJSONX() : null;
    

提交回复
热议问题