Thursday, April 19, 2012

Final Local Variable may not have been initialized in anonymous inner class

Here is my code:

final Foo myFoo = new Foo(new Inner() {

Java is giving me an error about how myFoo might not have been initialized. Is there any way to fix this? I can potentially set the callback to null when I construct the object and then change it afterwards, but I'd hope for there to be a cleaner way. Any ideas? (Also that wouldn't work if Foo wasn't written by me and didn't expose an interface to change the callback later)

In case anyone is curious, in my specific scenario, Foo is an ArrayAdapter, and the callback is notifyDataSetChanged(). What is displayed by the adapter depends on the values of the items in the array, and those values change when they are clicked on. The callback is the clickListener.

No comments:

Post a Comment