Is the use of Java's default package a bad practice?
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.
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.
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.
Java compiler imports java. lang package internally by default. It provides the fundamental classes that are necessary to design a basic Java program.
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.
There are problems on many different levels:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With