It's possible to make anonymous classes have new fields in Java:
class A {
public static void main(String[] args) {
Object o = new Object() {
public int x = 0;
{
System.out.println("x: " + x++);
System.out.println("x: " + x++);
}
};
System.out.println(o.x);
}
}
But they can't be accessed in this example, because o is of type Object:
$ javac A.java && java A
A.java:10: cannot find symbol
symbol : variable x
location: class java.lang.Object
System.out.println(o.x);
^
1 error
Note that o is actually not an Object, it's an instance of a different class extending Object. This class has one public field x. Does this class have a name? How can I make o be the proper type so I can get access to o.x? (Note that it's possible to do this with reflection, but I want to do it statically).