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;