OK, that’s how they do it. Take this sample class:
package hidden; class YouCantInstantiateMe{ private YouCantInstantiateMe(){ System.out.println("Damn, you did it!!!"); } }
The above package is a private class with a private constructor in another package, but we will create it anyway:
Code (executed from a class in another package):
public static void main(final String[] args) throws Exception{ Class<?> clazz = Class.forName("hidden.YouCantInstantiateMe");
Output:
Damn you did it!
Of course, which makes Spring a lot more complicated, because they also deal with design injection, etc., but this is how to create invisible classes (or invisible constructors).
Sean Patrick Floyd
source share