Is the use of Java's default package a bad practice?

Is the use of Java's default package a bad practice?


Solution 1:

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.

Solution 2:

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

Solution 3:

Yes, it is. The problem is that it is not possible to import classes from the default package.