-3

I use Vaadin for my project and I have a question: How change variable external class franchSize?

                TextField franchSize = new TextField();
                franchSize.setDebugId("franch_size");
                hl1.addComponent(franchSize);
                franchSize.setValue("0");
                hl1.setComponentAlignment(franchSize,
                        Alignment.MIDDLE_CENTER);
                franchSize.addListener(new Property.ValueChangeListener() {
                    private static final long defaultValue = 0;
                    public void valueChange(ValueChangeEvent event) {
                        String value = (String) event.getProperty().getValue();
                        if(Integer.valueOf(value)%1==0){
                            franchSize.setValue("0");
                            franchSize.getWindow().showNotification("","Bla-bla-bla",Notification.TYPE_HUMANIZED_MESSAGE);
                        }
                    }
                });

Error: "Cannot refer to a non-final variable franchSize inside an inner class defined in a different method"
in "franchSize.setValue("0");" and
"franchSize.getWindow().showNotification("","Bla-bla-bla",Notification.TYPE_HUMANIZED_MESSAGE);"

viartemev
  • 211
  • 1
  • 4
  • 13

4 Answers4

1

Here is a simple implementation for communicating with outer class variable,

public class Google01 implements Outer{
    // outer class variable, see no final here
    int outer = 0;

    public static void main(String[] args) {
        Google01 inst = new Google01();
        inst.testMe();
    }

    public void testMe(){
        // Inner class
        class temp{
            Outer out;
            public temp(Outer out) {
                this.out = out;
            }
            public void printMe(String text){
                // reading outer variable
                System.out.println(out.getValue() + text);
                // setting outer variable
                out.setValue(out.getValue() + 1);
            }
        }

        // Lets start our test
        temp obj = new temp(this);
        System.out.println("Value of outer before call = " + outer);
        // this should increment outer value, see number before Yahoo in output
        obj.printMe("Yahooo");
        obj.printMe("Google");
        obj.printMe("Bing");
        // Lets print outer value directly.
        System.out.println("Value of outer after call = " + outer);

    }

    @Override
    public void setValue(int value) {
        outer = value;
    }

    @Override
    public int getValue() {
        return outer;
    }
}

// An interface that is use to communicate with outer class variable.
interface Outer{
    void setValue(int value);
    int getValue();
}

Output

Value of outer before call = 0
0Yahooo
1Google
2Bing
Value of outer after call = 3

Brief Explaination: You need to make an interface in order to talk to outer class ( I like making interface for communication but it can be done using passing outer class instance also instead of making whole new interface), and you can use the utility method provided by interface in order to get or put the value in outer class. You can refer this to know why your error occur. Only thing that i have changed in the scenario is that i have made the variable as class level variable (member variable) from method variable.

Community
  • 1
  • 1
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • aren't you just passing in the variable into the innerclass (he/she could do the same by passing the `franchSize`)? Also `TextField` is a vaadin class and wrapping it again is overkill given that the value is there already from two sources. – cfrick Aug 19 '14 at 09:25
  • Yes i am just passing variable, since we can't use outer variable directly in inner class. – Gaurav Gupta Aug 19 '14 at 09:29
0

make franchSize final -- as stated in the error message -- or use (TextField)event.getSource() instead of referencing the outer variable.

for informations about the error search SO

Community
  • 1
  • 1
cfrick
  • 35,203
  • 6
  • 56
  • 68
0

The error is pretty clear here: you cannot access a external variable inside an inner class without making it final. So just add final in front of TextField franchSize = new TextField();

ortis
  • 2,203
  • 2
  • 15
  • 18
0

Just create a (private) field 'TextField franchSize' in the outer class. Best way to access it then, is to use a protected getter.

riddy
  • 501
  • 1
  • 4
  • 17