I've got an enum MyEnum in the default package, and I'm trying to import its instances in another file:
The result is a compiler error: The import MyEnum cannot be resolved. When I put MyEnum in a package, it works flawlessly.
I've googled a little on the subject and apparently, this is expected behaviour. Static import of something in the default package was possible in previous versions of Java, but for some reason Sun decided to disallow it. My question: why?
Yes, I normally put all of my classes in packages, I just want to understand the reasoning behind this decision.
Well, static imports from the default package were never possible, but it used to be possible to do regular imports from there. The default package is convenient for school lessons and throwaway examples (like most of the code you see in forums like this), but it never should have been used in real-world apps; it defeats the purpose of having packages in the first place.
The idea of the "default package" in Java was probably not a good idea, but once a feature has been added to the language, it is very difficult to take it out again, because it would break compatibility with older versions of Java.
Disallowing importing from the default package (which was introduced in Java 5 if I remember correctly) is one of the very few changes that breaks backward compatibility. Hopefully, not many people are implementing serious software in the default package.