• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

For all you language buffs: why is static import of something in the default package impossible?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jan Van den bosch
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They might as well get rid of the default package altogether then. Seems like a, pardon my language, half-arsed feature to me.
 
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be half-arsed, but it makes it easier for beginners who have enough difficulty learning how to compile a class without having to remember its package name.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic