Community
Participate
Working Groups
The compiler seems to generate the wrong code (or different from jdk 1.4.2_06, at least) for the default constructor in private nested classes. Given the following classes defined over two files: package scribble; public class Bar { public static void main(String[] args) throws Exception { Foo foo = new Foo(); foo.frob(Baz.class); } private static class Baz { } } public class Foo { public void frob(Class cls) throws Exception { Object o = cls.newInstance(); } } Compiling and running with the JDK gives: Exception in thread "main" java.lang.IllegalAccessException: Class scribble.Foo can not access a member of class scribble.Bar$Baz with modifiers "private" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.Class.newInstance0(Class.java:302) at java.lang.Class.newInstance(Class.java:261) at scribble.Foo.frob(Foo.java:5) at scribble.Bar.main(Bar.java:10) Compiling with Eclipse and running with the JDK gives no error. Comparing the output of javap shows why. javac gives us this: Compiled from "Bar.java" class scribble.Bar$Baz extends java.lang.Object{ private scribble.Bar$Baz(); } while Eclipse's compiler gives us this: Compiled from "Bar.java" class scribble.Bar$Baz extends java.lang.Object{ scribble.Bar$Baz(); } Interestingly, the same code is generated even if Baz has an explicit private constructor.
This happens only with private constructors; private methods have the private flag in the .class. Why this assymmetry?
This behavior has been in for a long time. But I agree it is wrong in the end. Will correct it. Marking as dup of bug 117758 which had more discussions in it. *** This bug has been marked as a duplicate of 117758 ***
Added InnerEmulationTest#test124