Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

packages

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

like image 630
H2ONaCl Avatar asked Oct 21 '11 12:10

H2ONaCl


People also ask

Why is the use of the default package discouraged?

If you create a class in a default package, JDT warns you "The use of the default package is discouraged". This is for good reasons, because the concept of "default package" is oddly defined in Java and can confuse even seasoned developers.

What does default package mean?

The default package is a collection of java classes whose source files do not contain and package declarations. These packages act as the default package for such classes. It provides the ease of creating small applications when the development of any project or application has just begun.

What are the best practices while using package in Java program?

Naming conventions and best practices for packagesThe name of the package should be in small letters. It is suggested to start the name of the package with the top level domain level followed by sub domains, ex: com. example. tutorialspoint.

Which package is important by default in Java?

Java compiler imports java. lang package internally by default. It provides the fundamental classes that are necessary to design a basic Java program.


2 Answers

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.

like image 157
Matt Ball Avatar answered Sep 21 '22 15:09

Matt Ball


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
like image 38
Sean Patrick Floyd Avatar answered Sep 19 '22 15:09

Sean Patrick Floyd