15

I don't understand why Java uses the reverse of a (probably hypothetical) domain name as the name of a package, while mostly there is no connection between the domain name that some people uses and the products that they have. A lot of developers don't even have any domain.

What are the reasons of this naming convention, if any?

1
  • Even without a domain it's common practice in Java-land to pretend you do for package naming. E.g. in your case you would use com.louisrhys.xxx.yyy whether or not you own louisrhys.com Oct 31, 2011 at 15:45

3 Answers 3

16

Global uniqueness. If everyone, or at least serious developers who distribute their code beyond in-house projects, adheres to that convention, it will never happen that you get name clashes when you add another third-party library to your project. Bear in mind that Java was initially propagated as a solution for code deployment anywhere, anytime (via applets and remote classloading over the internet).

4
  • 3
    In Java 1.4 Sun used open source Apache XML tools without changing the name space. Made it rather "interesting" to try to have a newer version in your own applications.
    – user1249
    Oct 31, 2011 at 8:45
  • 3
    Java was initially propagated as a solution for code deployment anywhere, anytime As opposed to what Java is now? I don't know about you but I still somewhat successfully use Java WebStart to deploy client side code to thousands of PC's in an internal network. This makes "Release Early, Release Often" far less painful for everybody.
    – maple_shaft
    Oct 31, 2011 at 12:07
  • 2
    Not necessarily true. You will eventually lose a domain name; whether by dying or just forgetting to renew it. Someone else could buy it, and, without realizing, put out a Java package that conflicts with yours. And there's also the possibility that someone owned the domain before you and put out code that your code conflicts with.
    – Kevin
    Feb 14, 2016 at 22:19
  • @maple_shaft It hasn't been that for most users since the decline of Java applets. Rather it's just another platform for applications (like, say, Qt or XUL or Electron).
    – user253751
    Dec 4, 2016 at 23:23
12

As Wikipedia says on the subject,

"The Java Language Specification establishes package naming conventions to avoid the possibility of two published packages having the same name."

4
  • Does the specification detail what convention to use if the developer doesn't have their own domain name? Oct 31, 2011 at 14:46
  • @FrustratedWithFormsDesigner: No it doesn't. Oct 31, 2011 at 15:08
  • @MikeSeymour: So... we can name them however we want in that case? Woohoo! :) Oct 31, 2011 at 15:17
  • 1
    @FrustratedWithFormsDesigner You can name them however you want in any case. If you want other people to use your software you should follow the specification and infer a logical and likely unique namespace if you don't have an actual domain.
    – Jeremy
    Oct 31, 2011 at 15:47
2

I found two Oracle-published documents that discuss the naming of packages. There is the Naming a Package page in the Java Tutorials and the Packages section of the Java Language Specification.

The primary purpose of this convention is to try to minimize conflicts between packages published by different organizations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.