10

If I declare following sealed hierarchy

package a;

import b.B;

public sealed interface A permits B {

}
package b;

import a.A;

public record B() implements A {

}

without using modules (no module-info.java) and try to compile it with Maven I get

[ERROR] .../src/main/java/a/A.java:[5,35] class a.A in unnamed module cannot extend a sealed class in a different package

I'm aware of https://openjdk.java.net/jeps/409 and this section:

The classes specified by permits must be located near the superclass: either in the same module (if the superclass is in a named module) or in the same package (if the superclass is in the unnamed module).

However, shouldn't Maven by default use classpath while compiling? Can this limitation be avoided at all?

If not, doesn't this set a precedent where a feature on module path is more flexible than on class path and that in turn - while classpath is still supported, it's not as first class citizen as it used to be when compared to module path?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lovro Pandžić
  • 5,920
  • 4
  • 43
  • 51

1 Answers1

20

The classpath is the unnamed module.

The motivation is that a sealed class and its (direct) subclasses are tightly coupled, since they must be compiled and maintained together. In a modular world, this means "same module"; in a non-modular world, the best approximation for this is "same package".

So yes, if you use modules, you get some additional flexibility, because of the safety boundaries modules give you.

Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
  • So in essence, after Java 9 it isn't possible to compile something without using the modules? This was the main detail I wasn't sure about. – Lovro Pandžić Jul 26 '21 at 05:02
  • 5
    @Ipandzic No, that's not right. Sealed classes work just fine without modules; it just means the boundary of co-maintenance is the package. If you work with modules, you get more latitude, the boundary is all the packages in that module. You can think of this as a positive interaction; you can use sealing without modules, or modules without sealing, or both at once, and if you use them together, you get some bonus flexibility. – Brian Goetz Jul 26 '21 at 16:28
  • 3
    Besides: you can't **really** work "without modules" in Java 9 and later. The closest you can come is to stuff a bunch of stuff into the unnamed module (i.e. the classpath). While that's pretty close, it's not exactly the same as disabling modules entirely. For example the classes of the JDK itself will still be in modules. – Joachim Sauer Jul 30 '21 at 10:22