How to use Outer Method's input in Anonymous Inner Class?

落爺英雄遲暮 提交于 2019-12-06 16:23:50

The variables declared within a method are local variables. e.g. hasTypedSomeToken and btnLogIn are local variables in your display method.

And if you want to use those variables inside a local inner class (classes that are defined inside a method e.g. the anonymous class that implements ClickHandler in your case) then you have to declare them final.

e.g.

void display(final boolean hasTypedSomeToken) {

If you look at Login.this.hasTypedSomeToken, this is used to access member variables. Local variables are not members of class. They are automatic variables that live only within the method.

First of all, you have to make it final:

void display(final boolean hasTypedSomeToken)

Then you can refer to it simply as hasTypedSomeToken:

if (hasTypedSomeToken) ...

You need to declare it final, like this void display(final boolean hasTypedSomeToken), and use it without prefixes: if(hasTypedSomeToken).

Make the variable final:

public class Login {

    void display(final boolean hasTypedSomeToken) {
        Button btnLogIn = new Button("Login", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                if (hasTypedSomeToken) {
                    // frob a widget
                }
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!