Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting access of classes in Java

Tags:

java

sdk

I'm writing an SDK in Java and I don't want every class to be accessible for the user to use. Making the class package-private doesn't help because I have hundreds of classes that I want to order in different packages.
Is there any way to restrict a class access for users who use my code as a library? Or maybe make the IDE warn the user when he tries to use specific classes in the library?
Thanks!

EDIT: I just want the user to know somehow that a class is internal and is not supposed to be used by him. Documenting the class isn't enough.

like image 994
Tal Goldrat Avatar asked Oct 30 '25 05:10

Tal Goldrat


1 Answers

If you have Java 9 at least, You can use Modules from JSR-376 standard. It's JPMS (Java Platform Module System).

Package-private feature could be not enough for SKD, because Users can avoid this access type by reflection.

like image 78
Łukasz Olszewski Avatar answered Nov 01 '25 20:11

Łukasz Olszewski