Java: Anonymous inner class using a local variable

后端 未结 5 1391
小鲜肉
小鲜肉 2020-12-06 02:49

How can I get the value of userId passed to this method in my anonymous inner subclass here?

public void doStuff(String userID) {
    doOtherStu         


        
5条回答
  •  眼角桃花
    2020-12-06 03:15

    Sure you can assign it as final - just put that keyword in the declaration of the parameter:

    public void doStuff(final String userID) {
       ...
    

    I'm not sure what you meant about it being a variable with an unknown value; all that final means is that once a value is assigned to the variable, it cannot be re-assigned. Since you're not changing the value of the userID within your method, there's no problem making it final in this case.

提交回复
热议问题