1

For Instance how can I use the input 'hasTypedSomeToken' in my Anonymou inner class in the following -

    public class Login {

        void display(boolean hasTypedSomeToken)
        {
           //some code here

               Button btnLogIn = new Button("Login", new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {

                    if(Login.this.hasTypedSomeToken) //HOW TO USE hasTypedSomeToken HERE 
                    {

                    //do something

                    }
                }
          }
      }
abhihello123
  • 1,668
  • 1
  • 22
  • 38

4 Answers4

3

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) ...
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

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

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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
                }
            }
        });
    }
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • I got 3 superfast answers and 1 elaborated answer in so less time. Im feeling like choosing among one of my own children :( Since this answer has some more explanation attached to it (& Iv to chose one) Im choosing it but thanks to u all equally – abhihello123 Dec 16 '11 at 15:35