Observation: Module imports and java.io.IO

Brian Goetz brian.goetz at oracle.com
Thu May 16 14:44:59 UTC 2024


 This was discussed, but only briefly.

I agree that it is sad that two import statements are needed rather than one.  But the alternative is worse.  For many static methods, the class name is really a part of the method name; static-importing “of” or “fromString” (which is a very common static method name) would be a terrible idea in almost all cases.  For these methods, they are intended to be invoked as “Date.fromString(s)”; the qualifier is really part of the name. But any kind of bulk-importing of static method names would likely sweep up such inappropriate methods.

We explored several ways to provide a controlled static-import that would come along for the ride with package or module import, but each of them seemed likely to cause as many problems as they solved.  So in the end, we opted not to generalize, and instead focused on the very small number of static methods that deserve to “look like they are built into the language.”

On May 14, 2024, at 12:26 PM, Eirik Bjørsnøs <eirbjo at gmail.com<mailto:eirbjo at gmail.com>> wrote:

Hi,

JEP-477 includes the section "Growing a program", showing how an implicit class can be grown into an ordinary class by wrapping it in a class declaration and adding the necessary (no longer implicit) imports:

import static java.io.IO.*;
import java.util.List;          // alternatively: import module java.base;

The module import of java.base (from JEP-476) strikes me as very elegant in this context in that it provides the learning programmer access to Java's basic APIs in a single import. "Give me the basic java stuff", thanks!

The sad thing is that the "basic java stuff" does not seem to include the static methods in java.io.IO<http://java.io.io/>. So the learning programmer still needs to add "import static java.io.IO.*". This doubles the number of imports needed, but worse, it introduces the concepts of static imports and wildcard imports!

I was thinking it would be really nice if "import module java.base" somehow also included the new IO methods.

Has this interaction between JEP-177 and JEP-476 been discussed in the expert group?

Thanks,
Eirik.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240516/5509d1c9/attachment.htm>


More information about the amber-dev mailing list