I have two projects/modules linked in Android Studio. One of them acts as an Api/Core, and I need that certain classes can by accessible only from this module but accesibles from all its packages (so “default” in this case is useless).
But it seems that in Java it isn’t a modifier like internal (c#, swift). “Module” modifier was proposed for Java 7 but it wasn’t implemented.
So, is there any way to protect the classes of a module from the outside world without limiting their scope to the current package?
Thanks!
There is no such an access modifier in jdk, but kotlin comes with internal
modifier, which does precisely what you want: the class becomes public within the module, but is not accessible outside of the module.
For detailed explanation of differences between modifiers in Java and Kotlin refer to Fragmented podcast's episode #101 - "Learning Kotlin – visibility modifiers, internal modifier, modules", as well as the official docs.
Not until Java 9. The Java 9 module system solves this exact problem. Another answer mentions Kotlin internal
scope, but a) the OP didn’t ask for a Kotlin solution, and b) I’ve found the Kotlin internal
scope not playing well with IntelliJ, which is ironical considering that JetBrains created both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With