Search code examples
javajarfactory-patterninformation-hidingfacade

Hiding classes in a jar file


Is it really impossible to hide some classes in a jar file?

I wanted not to allow direct instantiation of the classes to keep it more flexible. Only the factory (or a facade) should be visible of this jar.

Is there any other way than solve this problem than creating two projects? (Two projects: the first one contains the classes (implementation) and the other one references to the first one and contains the factory; later only the second one will be referenced)


Solution

  • I think you will have either compiler failure or warning if your public factory method try to return something which is "hidden".

    No, you can not hide a public class without reimplementing your own ClassLoader or using OSGi or anything similar.

    What you can do is to separate interface api from the implementation, e.g. have one project which contains only the interfaces and another porject which contains the implmentations. However, you still cannot hide the implementation classes.