Hi All,
Code example is from K&B but which shows how to create an anonymous inner class. BUT I am curious as to how a primitive can be accessed in the anonymous inner class:
****************************************
class Popcorn {
public int x = 2; // Line 1
public void pop() {
System.out.println("popcorn");
}
}
class Food {
Popcorn p = new Popcorn() { // Line 2
x=9; // Line 3 - compiler error!
public void pop() {
System.out.println("anonymous popcorn");
}
};
}
****************************************
Line 2 creates the anonymous subclass which according to
polymorphism should have access to public members of the superclass.
Why doesnt the above code compile? What am I doing wrong?
thanks