Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you really use your reverse domain for package naming in java? [closed]

Tags:

For a long time ago, I have thought that, in java, reversing the domain you own for package naming is silly and awkward.

Which do you use for package naming in your projects?

like image 518
Alex. S. Avatar asked Oct 09 '08 21:10

Alex. S.


People also ask

Why are package names reversed?

Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .

Why do Java package names start with com?

Generally we android developer having practice to decide package name based on the domain name of a particular company. For example: Domain name: sun.com => Root package name something like : com. sun.

Why do namespaces start with com?

It's just a namespace definition to avoid collision of class names. The com.

Can package name be camel case in Java?

The package names do not follow camel casing or underscores or hyphens package naming convention.


1 Answers

Once you understand why the convention exists, it shouldn't feel silly or awkward in the least.

This scheme does two important things:

  • All of your code is contained in packages that no one else will collide with. You own your domain name, so it's isolated. If we didn't have this convention, many companies would have a "utilities" package, containing classes like "StringUtil", "MessageUtil" etc. These would quickly collide if you tried to use anyone else's code.

  • The "reverse" nature of it makes class-directory layout very narrow at the top level. If you expand a jar, you'll see "com", "org", "net", etc dirs, then under each of those the organization/company name.

(added in 2021) This is even more important nowadays when this type of package naming is used for third-party libraries which are pulled in transitively during builds and could easily conflict if the names were not unique. If everyone adheres to the same convention, there will be no accidental collisions.

(added in 2021) The same naming convention can be used for application ids on an app store to ensure uniqueness as well.

We usually don't expand jars, but in early java development, this was important because people used expanded dir structures for applets.

However, this is nice now as source code dir structures have a very "top-down" feel. You go from the most general (com, org, net...) to less general (company name) to more specific (project/product/lib name).

like image 89
Scott Stanchfield Avatar answered Sep 28 '22 05:09

Scott Stanchfield