Search code examples
javajava-9

What is difference between requires public VS requires transitive in Java 9


I have learn that in order to use any other exported module, we need to specify that module as a requires:

module mymodule{
       requires othermodule.xyz;
}

When othermodule uses on thirdmodule and mymodule needs that as well, othermodule should define transitive dependency like this:

module othermodule {
       requires transitive thirdmodule  
}

However, I have seen many sites that use the public keyword for this situation:

module othermodule {
       requires public  thirdmodule  
}

What is the difference there is between the two forms; i.e. requires public and requires transitive?


Solution

  • The requires public was part of an earlier version of the modules sub-language that was revised before the release of Java 9. According to the Java 9 JLS, the public keyword is not allowed in a ModuleDirective: see JLS 7.7.

    The sites that use the requires public syntax are out of date / incorrect. This is acknowledged in the following, for example:

    Basically requires public is outdated (now invalid) syntax, and requires transitive is its replacement.