JEP 476: Clarification on Package Export Behavior in Java Modules

Thiago Henrique Hupner thihup at gmail.com
Fri Sep 13 12:00:50 UTC 2024


Dear Amber Team,

I hope you're doing well. I’ve encountered a behavior related to JEP 476:
Module Import Declarations and wanted to ask for clarification.

I’m working with a module setup where two packages are defined but not
initially exported:

module my.nice.project {
    //exports my.nice.project.pkg.a to my.nice.project;
    //exports my.nice.project.pkg.b to my.nice.project;
}

Here’s a simplified version of the code structure:


package my.nice.project;

import module my.nice.project;

public class Main {
    public static void main(String[] args) {
        SomeInterface someInterface = new SomeInterface() {
            @Override
            public SomeRecord someMethod() {
                return new SomeRecord();
            }
        };
    }
}


package my.nice.project.pkg.a;import module my.nice.project;

public interface SomeInterface {
    public SomeRecord someMethod();
}

package my.nice.project.pkg.b;public record SomeRecord() {}

When attempting to compile this code with the exports commented out, I
received several "cannot find symbol" errors. However, after uncommenting
the exports:


exports my.nice.project.pkg.a to my.nice.project;exports
my.nice.project.pkg.b to my.nice.project;

The code compiled and ran as expected.

Given this behavior, I wanted to confirm if this restriction on package
visibility within a module is the intended design under the Amber project
and the module system, or if there’s another approach or best practice I
might be overlooking in terms of package exports.

Thank you for your time and insight.

Best regards,

Thiago
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240913/30535223/attachment.htm>


More information about the amber-dev mailing list