How does the “final” keyword in Java work? (I can still modify an object.)

前端 未结 18 2330
醉酒成梦
醉酒成梦 2020-11-22 03:08

In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of

18条回答
  •  鱼传尺愫
    2020-11-22 03:38

    Read all the answers.

    There is another user case where final keyword can be used i.e. in a method argument:

    public void showCaseFinalArgumentVariable(final int someFinalInt){
    
       someFinalInt = 9; // won't compile as the argument is final
    
    }
    

    Can be used for variable which should not be changed.

提交回复
热议问题