I have an object that I want to be modified when I call the appropriate instance method for it. (I think that is the correct vocabulary)
From one class, I am doing this:
Pizza pizza = new Pizza();
pizza.addTopping(Cheese);
pizza.addTopping(Pepperoni);
pizza.setCrustType("thick");
pizza.make();
pizza.putInOven();
Here’s where things get interesting. So here’s the make() method that is called above that is in the class Pizza.
public void make()
{
final Pizza pizza = this;
pizza.registerUpdateHandler(new IUpdateHandler()
{
@Override
public void onUpdate(float cookTime)
{
// need to do some modifications to pizza
pizza.doSomething();
pizza.doSomethingElse();
}
});
}
In order to be able to access the pizza object inside the inner class, IUpdateHandler(), I have to set a Pizza object to this. But then, it also has to be final because you:
Cannot refer to a non-final variable inside an inner class defined in a different method.
Going back to my original code snippet, I still need to call pizza.putInOven() and have it be referencing the same Pizza object as all the other calls, but with the modifications from the make() method in effect. Because I had to create a new Pizza object of type final in the method make() that is no longer the same object modified when I want to call putInOven().
How do I solve this issue? If my question is not clear, I’ll do my best to edit and restate, just leave a comment.
you don’t need the additional pizza variable because the code is already executed inside the pizza object.