JAR level (assembly level) scope in Java

In C #, if I want a class to be visible to any class inside that assembly (DLL), I just treat it as internal

(which is the default).

How can I do this in Java? In Java, I noticed that the default / internal scope is the package level, not the JAR level. This is a problem for me as I have a library that has several sub-packages with different responsibilities (view, controller, etc.) and cannot put them in the same package.

As an example, I have two classes like com.stackoverflow.main.first.One

and com.stackoverflow.main.second.Two

, both of which should be able to create each other for each other.

Edit: I don't want the class to be public

and visible from everyone who links to it. This is only an inner class. I am creating an API for consumption and what matters to me is what classes the consumers of my JAR can see.

+2


source to share


3 answers


To accomplish what you want, you will need to use a combination of the factory pattern to create the classes you want to expose and leave the private package private. I used to do this:

  • create a public interface for the API in a package like com.foo.bar

    a lapublic interface Foo {}

  • create a factory class that provides a create a la method: public class FooFactory{ public Foo buildFoo(){ return new FooImpl(); }

  • create FooImpl as private package class - class FooImpl implements Foo{}

  • A package of documents to indicate the correct use.


It's not perfect, but until the module walkthrough JSR comes through, it will probably be the closest thing to java. If you want FooImpl not to receive an inappropriate extension, make sure it is marked final.

+1


source


Java has no idea about the library level. Make the classes public or use a factory.



+3


source


sounds as simple as you need to use the open access modifier.

0


source







All Articles