Is using a default Java package a bad practice?

Is using a default Java package a bad practice?

+48
java packages
21 Oct. '11 at 12:31
source share
4 answers

Yes it is. Ideally, package names should be globally unique to avoid name collisions. Using the default package violates this agreement. It is also not possible to import a class from the default package.

Why do nameless packages exist at all, if such a bad idea? From JLS & sect; 7.4.2 :

Visa-free packages are provided by the Java platform mainly for convenience when developing small or temporary applications or when development is just beginning.

+54
Oct. 21 '11 at 12:35
source share

There are problems at different levels:

  • you cannot import classes into the default package from classes that are not
  • You will get problems loading classes if you try to resolve the default package in multiple artifacts.
  • you can no longer use the standard and protected area, as usual,
  • There is no clear distinction between your code and other code.
+20
Oct 21 '11 at 12:35
source share

Yes it is. As the other answers pointed out, you cannot load classes from the default package.

See SO answers:

How to import a class from the default package
How to access java classes in a package by default?

However, log4j requires that the configuration be located in the default package. This is the only thing that is reasonable to keep there.

Edit:. As Sean Patrick Floyd and Michael note in the comments, you can put any configuration files in the default package. Thank you both for pointing this out to me.

+3
Oct 21 '11 at 12:38
source share

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

+1
Oct 21 '11 at 12:34
source share



All Articles