Here is a sample program tested in Java 1.5.
I wonder why the two approaches below have different result. Is it a bug or a kind of Java feature?
package test;
public class TestOut {
public static void main(String[] args) {
// works
new TestIn();
// throws IllegalAccessException
Class.forName("test.TestOut$TestIn").newInstance();
}
private static class TestIn {
}
}
This is Bug ID 4221909:
The class is private, hence the
IllegalAccessException
- you can use:For the record, the exception has a message, which is quite descriptive. Next time don't omit such information from the question. (actually, I'm not sure this message exists on Java 1.5, does it?)
The problem lies in the
verifyMemberAccess(..)
method ofsun.reflect.Reflection
, and that it doesn't take into account enclosing classes. If a member (constructor) is private, access is denied.