Java Reference
In-Depth Information
IllegalAccessException is that this class is not public and comes from another package: You
cannot legally access a member of a nonpublic type from another package [JLS 6.6.1].
This prohibition applies whether the access is normal or reflective. Here is a program that runs
afoul of this rule without resorting to reflection:
package library;
public class Api {
static class PackagePrivate {}
public static PackagePrivate member = new PackagePrivate();
}
package client;
import library.Api;
class Client {
public static void main(String[] args) {
System.out.println(Api.member.hashCode());
}
}
Attempting to compile the program results in this error:
Client.java:5: Object.hashCode() isn't defined in a public
class or interface; can't be accessed from outside package
System.out.println(Api.member.hashCode());
^
This diagnostic makes about as much sense as the runtime error generated by the original reflective
program. The class Object and the method hashCode are both public. The problem is that the
 
 
Search WWH ::




Custom Search