0

I used a local variable to store this in order to make it accessible inside an inline overriding but not feeling happy about that. Is there a more elegant way of doing that?

public class Tree extends Plant {
    public Tree() {
        timeline = new Timeline(new KeyFrame(Duration.millis(40), new EventHandler<ActionEvent>() {
        final Plant tree = this; // <---- here
            @Override
            public void handle(ActionEvent actionEvent) {

                tree.setA(5); // <---- and here
            }
        }));

        timeline.setCycleCount(Timeline.INDEFINITE);
    }
}
ajeh
  • 2,652
  • 2
  • 34
  • 65

1 Answers1

2

You should use Tree.this. Since you're trying to access information about the Tree object inside of another class (i.e., EventHandler), this needs to be qualified so that the compiler knows what you're talking about.

liltitus27
  • 1,670
  • 5
  • 29
  • 46