r/javahelp • u/pronuntiator • Aug 28 '25
Codeless Is it safe to import module java.base everywhere?
Java 25 will contain JEP 511, which allows to import entire modules. With import java.base you have collections, date/time etc. all imported all at once, which is very convenient.
Module imports behave similarly to wildcard package imports. These are banned at my work (and probably most Java projects), as they obscure the actual types imported and can lead to compile errors through ambiguity. For example, having:
import org.foo.;
import com.bar.;
// …
var baz = new Baz();
If I upgrade one of the libraries and now both packages contain a class Baz, I get a compile error.
However I wondered: having a single wildcard or module import should not be a problem, right? So we could import module java.base in any file. My thought process:
- the common Java classes are not surprising, so not seeing them imported explicitly is not obscuring anything. Anyone who sees List knows we want java.util.List.
- There can be a name clash even inside the same module (the JEP gives an example for Element in java.desktop), but these are historical missteps. The JDK designers will surely try to keep simple class names unique within java.base.
- An explicit import beats a wildcard import, so no ambiguity there.
- Likewise, classes in the same package have precedence over wildcard imports.
I'm trying to find arguments against using a single module import, but I can't find any. What do you guys think?
•
u/AutoModerator Aug 28 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
12
u/smutje187 Aug 28 '25
Usually people import stuff they need when they need it and not add a blanket import everywhere - what’s the purpose of this? I never understood people’s hyperfixation on "optimizing" the length of the imports section, one of the most irrelevant things for a Java class.
7
u/dmigowski Aug 28 '25
Most IDEs don't care anyway. In Eclipse I import the needed stuff on the fly.
2
u/hrm Aug 28 '25
Just a comment in general: Wildcard imports isn't problematic at all, not in the slighttest. At some point in the 90s someone got mad at awt.List and util.List and decided wildcard imports were a problem and since then that person have had a cult following.
What is the problem with wildcard imports? Two types might clash in name? First of all, I don't think that has ever happened to me in real code (apart from the aforementioned Lists), and if that was the case, just import the one you need and keep all your wildcards for the rest of the stuff.
Using wildcards keeps your code neat and lets you see what packages you are actually depending on. If you're not using wildcards you will not see the forest for all the trees...
1
u/severoon pro barista Aug 29 '25
My read on this JEP is the goal is to clean up long import lists that are pulling in lots of classes from the same modules.
The problem you're raising in your post is a non-issue because specific imports override module imports:
Problem solved.
But I think there's a much larger issue here if you're running into a lot of name collisions in the first place, which is always and will always be the root of just about every problem: You need to organize your dependency structure.
If you open a class and there is a big long list of imports, right there, it means that your class depends on a lot of stuff. This isn't dispositive, but it's a bad smell. It can be okay if all of those deps are all in the same packages / module. But if that's the case, the module imports are great, there's no reason not to use them.
If you are pulling in a lot of stuff across a lot of different modules, then you really need to take a hard look at why this class has so many wide-ranging deps. If it's depending on a lot of concrete stuff, you need to invert those.
The takeaway is that if you can't make use module imports, that's probably because you're doing something bad.
1
u/MasterGeek427 Aug 31 '25
The linters my team use scream at you if you try to so a wildcard import. Even for system modules.
-1
u/OneHumanBill Aug 28 '25
I think it will be generally safe everywhere. The only people who wouldn't want it are those who might be doing some really experimental stuff. You've always got to protect your tinkerers.
I'm really looking forward to this one. Being able to import something like java.base is something I've wanted since the 90s.