Why do we use reverse domain name like com.something. or org.something. structure for java packages? I understand this brings in some sort of uniqueness, but why do we need this uniqueness?
-
3It's actually more in line with the convention in which left-to-right moves from the general to the specific -- think of file paths or the Dewey decimal system. You could ask, conversely, why domain names violate this convention (or why we write dates in a manner so unfriendly to sorting).– harpoCommented Mar 19, 2010 at 5:44
-
@harpo: True. Domain names and dates are better written in reverse.– Leo JwedaCommented Mar 19, 2010 at 6:17
-
The "server.domain.tld" syntax a very English-language way of expressing things (modifiers before the object - a "red ball"). Spanish, for example, has the more logical "object modifier" syntax ("bola roja").– dlchambersCommented Jul 2, 2021 at 12:15
6 Answers
About why we do it reversed: Imagine you have two important packages, an accounting package and a graphics package. If you specified these in 'straight' order:
accounting.mycompany.org
graphics.mycompany.org
Then it implies there is a major accounting
package, a subsection of which is for mycompany
, and a subsection of that package is called the org
package which you actually use. However, you want this:
org.mycompany.accounting
org.mycompany.graphics
This makes more sense. Out of all packages from organizations (org
), you look at mycompany
in particular, and it has two sub-packages, the accounting
and the graphics
ones.
-
10+1. Note that (unfortunately?) Java has no support for sub-packages, though. A subpackage is no closer to its parent (as far as visibility is concerned) than a completely unrelated package. It is of course still useful for code organization purposes.– ThiloCommented Mar 19, 2010 at 5:43
-
2It only implies that org is the concrete package because you don't really pull your example through completely. What if the first part is the concrete package? Would that be impossible? -1 Strawman, right-to-left reading countries would disagree right here :P– sinni800Commented Sep 12, 2013 at 11:24
-
1@sinni800: -1 to your comment. when you write code in Java, it's written left-to-right. Further, when you have
a.b.c.d
, it means you start off with classa
, nesting into its fieldb
, nesting to that field'sc
, then nesting to that field'sd
. Analogously package organization should work the same. Also in Python packages subpackaging actually is done the way I wrote out here. So it wouldn't be impossible but it would be moronic.– ClaudiuCommented Sep 12, 2013 at 15:12 -
@Claudiu But there are some portions of the language (inherited from the C-style syntax) where, while there is left-to-right reading, there is a right-to-left style (in terms of words, not characters. For example function definitions. Return type comes first, even though as a human, you wouldn't put that first.). I'm just trying to tell that there isn't an universal truth that sub pakages have to go from left to right. I didn't know about the python thing, though.– sinni800Commented Sep 12, 2013 at 16:41
-
@sinni800: hmm well yeah everything's a matter of convention. the left->right being general->more specific is quite common though... can you name any programming languages that has packages and doesn't do it left to right?– ClaudiuCommented Sep 12, 2013 at 17:07
Globally unique package names avoid naming collisions between libraries from different sources. Rather than creating a new central database of global names, the domain name registry is used. From the JLS:
The suggested convention for generating unique package names is merely a way to piggyback a package naming convention on top of an existing, widely known unique name registry instead of having to create a separate registry for package names.
-
5DNS really doesn't have anything at all to do with it (+1 anyway)– FredrikCommented Mar 19, 2010 at 5:45
-
6@Fredrik no DNS lookups are actually performed, but the "widely known unique name registry" referred to in the JLS is "the domain name system". Commented Mar 19, 2010 at 6:19
As you say, reverse domain names as base package name ensures uniqueness. Suppose two companies with DN example.com and example.org both define the class Employee in their framework. Now if you are using both frameworks you will not be able pinpoint which Employee you want to use in your code, but if they are defined in packages com.example and org.example respectively you can tell the compiler/JVM specifically which class you are referring to. If unique packages are not defined you will get compilation errors or runtime errors, e.g. if you are using the com employee class, but the org employee class gets loaded first from the classpath you will get a runtime error, since the two employee classes may not have same structure.
The uniqueness is needed for Class Loading.
It helps by avoiding naming collisions. If there are classes with same package name and class name, Collision will occur while trying to load the classes.
This generally happens if there are multiple libraries(jar) that contain classes with same names.
Also see this.
-
What sort of loading are you talking about? and how does naming it uniquely prevent a possible collision? I assume that whatever conflict occurs during loading, occurs if the class name is also same. Commented Mar 19, 2010 at 5:38
-
@Vaishak: That assumption is false. The unique name identifying the class is the package + the class name. If you're in the same package or if you have specifically imported a package or fully qualified class name you can use the name only. Much like with dialing a phone, you normally don't need to specify area code if you're in the same one already but the full phone number that you hand out would still include the area code.– FredrikCommented Mar 19, 2010 at 5:43
-
@Fredrik My assumption was in line with your explanation :) That is why I said conflict occust if the class name is also same. But my question has been answered now. Thanks! Commented Mar 19, 2010 at 5:58
You need the uniqueness if you might need to integrate your code with third party software, or provide it to someone else for integration. If you don't follow the rules, you increase the risk that at some point you will have a class naming collision, and that you will need to rename lots of your classes to address it. Or worse still, that your customers will have to do the code renaming.
This also applies when code is produces as part of different projects in an organization.
As you said, it brings uniqueness, something that is needed especially when working with third party code. For example, consider that you are using a library I've made. I've used the package "foo" and have a class named Bar there. Now if you are also using the package name "foo" AND you have a class named Bar, this would mean that your implementation would override my Bar implementation, making my implementation inaccessible. On the other hand, if my package were "com.mydomain.foo" and I'd had by Bar class there, then you can freely use the name Bar in one of your classes and both classes could still be uniquely identified and used separately.
Why use the reverse domain name as the package name? I guess that is just a convention to make sure that everybody uses a unique namespace, as you shouldn't use someone else's domain in your package name.