6

I'm trying to use best practices that fit with Java 9 module system so that I have less work to do to get the benefits (and our system right now could really benefit from some modularity).

Is it permissible under the current standard for module A to export the package com.example.foo and also for module B to export the package com.example.foo?

As a related question, if relevant, is whether this point is actually settled or if it's still not final.

Naman
  • 27,789
  • 26
  • 218
  • 353
Kevin Peterson
  • 7,189
  • 5
  • 36
  • 43
  • 2
    No idea but I doubt you should start doing such a thing with modules because it still violates https://en.wikipedia.org/wiki/Package_principles (esp "inseparable" in CRP) – zapl Dec 05 '16 at 23:55
  • 1
    I presume you mean for A and B to both be loaded in the same JVM at the same time? – Jason Dec 06 '16 at 00:52
  • 2
    To the closers of this question: The linked question is about non-exported packages, and this question is about exported packages. I believe the difference is relevant. – Jason Dec 06 '16 at 14:00
  • 1
    @jay Yes, they are definitely different questions. I'm not sure how to "edit this question to explain how it is different" when my title uses the word "export" and the supposed duplicate is explicitly about "non-exported". – Kevin Peterson Apr 03 '17 at 23:54

1 Answers1

10

According to the latest State of the Module System, the module system ensures that "every module reads at most one module defining a given package, and that modules defining identically-named packages do not interfere with each other." And: "When code in a module refers to a type in a package then that package is guaranteed to be defined either in that module or in precisely one of the modules read by that module."

This means that two different modules may export the same package if - at build time and at run time - no module depends on both A and B at the same time and if A and B don't depend on each other. In theory you could have two modules that export the same package, and use them one at a time with another depending module.

I would also suggest, as has already been suggested, that it is best practice to have a package exported by a single module instead of two.

Jason
  • 7,356
  • 4
  • 41
  • 48
  • 1
    I guess this implies that something like a logging framework with multiple possible dependencies providing the same class would be acceptable, but pretty much everything else needs to be consistently divided. – Kevin Peterson Sep 06 '17 at 21:43