Java: Anonymous inner class using a local variable

后端 未结 5 1410
小鲜肉
小鲜肉 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 02:54

    declare the method

    public void doStuff(final String userID)
    

    The value needs to be final so that the compiler can be sure it doesn't change. This means the compiler can bind the value to the inner class at any time, without worrying about updates.

    The value isn't changing in your code so this is a safe change.

提交回复
热议问题