Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visibility scope project/module

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!

like image 896
Víctor Albertos Avatar asked Feb 27 '15 12:02

Víctor Albertos


2 Answers

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.

like image 62
azizbekian Avatar answered Sep 29 '22 00:09

azizbekian


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.

like image 34
Abhijit Sarkar Avatar answered Sep 28 '22 22:09

Abhijit Sarkar