Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74755
336
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic