java.lang.IllegalAccessException upon reflective class instantiation

This evening I got the following exception when reflectively instantiating a class.

Caused by: java.lang.IllegalAccessException: Class foo.Baz can not access a member of class foo.Bar with modifiers ""
	at sun.reflect.Reflection.ensureMemberAccess( [na:1.6.0_16]
	at java.lang.Class.newInstance0( [na:1.6.0_16]
	at java.lang.Class.newInstance( [na:1.6.0_16]
	at [build/:na]
	... 90 common frames omitted

If you get this all you need to do is to make sure that the constructor of the class you are trying to instantiate is visible to the class that is doing the reflective instantiation. The empty modifier string in the exception message is confusing at first but simply means that your constructor is package private and therefore has no access modifier. If your constructor has an access modifier it will show within that string.

Credits: Troubleshooting the reflective API.

Leave a Reply