I experience an interesting mismatch between what Eclipse and my JDK consider legal java.
Eclipse compiles the following class without crashing, while the JDK on Mac OS X results in the error below.
public class Builder { private class Item {} public void addItem(Item i) {} public static void main(String[] args) { new Builder() {{ addItem(new Item()); }}; } }
$ javac Builder.java Builder.java:9: non-static variable this cannot be referenced from a static context addItem(new Item()); ^ 1 error
Creating a static Item class fixes the problem, but I was a little curious: Is Eclipse soft and compiles code that is not really valid? Am I stumbled upon the eccentricity of the Mac OS X JDK? Am I missing something?
Update It may be appropriate to include the following
$ java -version java version "1.6.0_33" Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720) Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
Update 2
Making the element more visible (by default, protected or public) also satisfies the JDK compiler.
source share