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

JavaPackages

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionH2ONaClView Question on Stackoverflow
Solution 1 - JavaMatt BallView Answer on Stackoverflow
Solution 2 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 3 - JavaMathias SchwarzView Answer on Stackoverflow