Is the use of Java's default package a bad practice?
Asked
Active
Viewed 3.3k times
66
-
I have vague memories of problems arising from using the default package when trying to refer to classes in that package from other, 'real', packages. – Oliver Oct 21 '11 at 12:33
-
4Yes, unless you are writing throw-away code. – aroth Oct 21 '11 at 12:34
-
6Depends on what you're doing. For one-class Java homework it's fine. Anything else - just put it in a named package and you are safe. – Goran Jovic Oct 21 '11 at 12:34
-
3"This sentence will probably get me past the quality check." If you need to add a totally useless sentence in order to post your question, it's likely not worth asking in its current state. – Anthony Grist Oct 21 '11 at 12:34
-
Basically, every Java class has a dedicated name composed of the package and the class name. If you don't have a package name, because you use the default package, the dedicated name is only the class name. Thus, you can use your class only from within your own project, because there is no proper way of addressing your class properly elsewhere. – Till Helge Oct 21 '11 at 12:35
-
6For some reason this got "close" votes- That is not correct. This question is not "opinion" based. This has solid reason and answers covers that well. Please spare this from "aggressive close votes" – Jayan Jul 03 '15 at 05:03
3 Answers
66
Yes, it is. Ideally, package names should be globally unique, to avoid naming collisions. Using the default package breaks this convention. It's also impossible to import a class from the default package.
Why do unnamed packages exist at all, if it's such a bad idea? From the JLS §7.4.2:
Unnamed packages are provided by the Java platform principally for convenience when developing small or temporary applications or when just beginning development.

Matt Ball
- 354,903
- 100
- 647
- 710
-
1...Until someone else has the same train of thought as you. What's the problem with just using a named package? – Matt Ball Oct 21 '11 at 13:08
-
-
3...yeah, but then they worried about people like me so they all decided not to use it. – irreputable Oct 21 '11 at 13:17
-
2
-
3Why is providing something that is essentially bad practice for the sake of convenience a good idea though? Why doesn't the standard/specification simply require all classes to have a package name as part of the minimum syntax needed, so there's no unnamed package to begin with? IDEs can maybe be internally configured to have a "default" named package called, for example, "project-name", "project-name-team-name" or some other preset string/template. If you're lazy, learning or simply don't need multiple packages, the IDE can encapsulate mandatory package naming. – thegreatjedi Feb 12 '16 at 03:39
-
I'm thankful to FINALLY find this information. I don't know why NO ONE ever told me this in any tutorial. I was having a hell of a time understanding why I was getting Error: Could not find or load main class Main. I could only solve the problem by adding a package - a work around - as I saw it. – chrips Aug 13 '17 at 08:54
-
Why does javac not throw a warning? Seems like a bit of an oversight if it's such a bad idea. – Paul Feb 16 '19 at 15:06
21
There are problems on many different levels:
- you can't import classes in the default package from classes that are not
- you will get class loading problems if you try to resolve the default package in multiple artifacts
- you can no longer use the default and protected scope like you normally can
- there's no obvious distinction between your code and other code

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
2
Yes, it is. The problem is that it is not possible to import classes from the default package.

Mathias Schwarz
- 7,099
- 23
- 28