The final local variable cannot be assigned

前端 未结 6 1989
说谎
说谎 2020-11-29 23:48

I have an array of seats, and the array has two strings(selected and empty). On mouse click, I want to traverse the array and find the selected seat. When I press the button

6条回答
  •  日久生厌
    2020-11-30 00:37

    Instead of defining a class member variable you can also use a mutable int to achieve the same.

    void foo() {
        final MutableInt a = new MutableInt(3);
        new Runnable() {
            @Override
            public void run() {
               a.add(3);
            }
        };
    }
    

    Since MutableInt is not primitive type (hence passed by reference) and can be reassigned this works.

提交回复
热议问题