Reputation: 25
I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application?
Package a MyClass1
Package b MyClass2
Package c public MySharedClass
Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the world, how could I prevent that?
Upvotes: 2
Views: 93
Reputation: 58
Create a package that is documented as an internal package, not to be used by clients.
There is no way in Java to make a class public only for certain packages: It either is public for everyone or package-private (public only in the declared package).
I think there's a proposal for modules to allow better control in the visibility of classes, but we'll have to wait, at least, for Java 8.
Upvotes: 1
Reputation: 111259
Packages in java are flat: if you want to access something in package c from package a, the API user in package x can also access it.
Make your packages big enough so that they can be self contained. If you find yourself in this situation you are trying to make packages that are too small.
Upvotes: 1
Reputation: 2907
You can make them protected classes
which will be accessible to your classes only.
Upvotes: 0
Reputation: 136022
We cannot prevent it. Java does not have jar-private access level.
Upvotes: 1
Reputation: 2541
You are looking for so called package-private modifier (which is accually no explicit modifier)
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Upvotes: 1