Java 9 scope of public access specifier has been changed. Because of a modular system in java public access specifier scope has been changed. Its little bit interesting let see here for more details with an example about how its will behaves in Java 9.

“public means not accessible every where !!”

Scope of public access specifier

Java 9 on words if any class is public it doesn’t mean that class can be accessible everywhere. Accessibility of  public class depends on module declaration and based on module declaration there are three possible scenarios as below:

  1. public only within a module
  2. public to everyone
  3. public but only to the specific module

1. public only within a module:

public only within a module means that public class can only accessible in within module in other words public class cannot be an accessible outside of the module.

When modules are not exporting package inside module declaration means class inside those packages will be accessible by other modules.

Example:

module com.developer.foo is not exporting any package means that every public class inside that module cannot be accessible by outside of the module.

module com.developer.foo{

}

2. public to everyone:

public to every module means that class can be accessible to every reader modules. It’s possible only when source module export those package, so public class available in that package can be accessible by reader modules.

Example:

we have two module com.developer.foo and com.developer.boo whose module-info.java as under:

module : com.developer.foo

here com.developer.foo module is exporting com.developer.service package for other modules. Reader modules can access public class of com.developer.service packages.

module com.developer.foo{
exports com.developer.service; // foo is exporting so it public class is can be accessible by other modules
}

module : com.developer.foo

Here module foo can access all public class which is insidecom.developer.service package because foo module is exporting that packages.

module com.developer.boo{
requires com.developer.foo   // reading foo module
}

3. public but only to specified module

In above example, we have seen that com.developer.service can be accessible every reader modules but we can allow to access to the specific module instead of all module with the export [ package-name ] to [module1,module2] option during module declaration.

Example:

We have three modules,com.javadeveloper.foocom.javadeveloper.boocom.javadeveloper.zoo, Here are module-info.java files.

module: com.developer.foo

module com.developer.foo{
exports com.developer.service to com.developer.boo; // only allowed to read boo module
}

module: com.developer.boo

com.developer.boo can access all public classes inside com.developer.service because foo module is exporting that package to a boomodule.

module com.developer.boo{
requires com.developer.foo  // reading foo module
}

module : com.developer.zoo

com.developer.zoo can not access com.developer.service package’s class because foo modules is only allowed to export to boo modules not to any other modules.

module com.developer.zoo{
requires com.developer.foo  // reading foo module
}

References:

http://openjdk.java.net/projects/jigsaw/

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *