The x=9 is in your anonymous inner class is like an instance variable, and in general, for an instance variable, you should mention the type.
Take a normal example as this:
Even in this code, you will get the same compiler error. So, you have to DECLARE the variable as an instance variable. But, you can access the inherited variable in any of the child methods in the normal way. For eg, in your Popcorn example, you can have like this:
System.out.println("anonymous popcorn" + x); // x will be 2 here, inherited from the parent Popcorn
Originally posted by Santiago Bravo: yes, if I had declared int x = 9; on line 3 then this is a totally new variable for the instance.
I can access variable x from the Popcorn class if I use it in a method in the anonymous inner class
Not sure what you are asking. Are you trying to assign the variable, without declaring a new one? If you are, then it is a statement, not a declaration. And statements are not allowed outside of an initializer, constructor, or method.