tags:

views:

246

answers:

6

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?

A: 

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.

Padmarag
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.
Vaishak Suresh
@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.
Fredrik
@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!
Vaishak Suresh
+9  A: 

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.

Laurence Gonsalves
DNS really doesn't have anything at all to do with it (+1 anyway)
Fredrik
@Fredrik no DNS lookups are actually performed, but the "widely known unique name registry" referred to in the JLS is "the domain name system".
Laurence Gonsalves
A: 

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.

Stephen C
+3  A: 

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.

Claudiu
+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.
Thilo
A: 

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.

Kim L
+1  A: 

As you say, reverse domain names as base package name ensures uniqueness. Suppose two companies with DN foo.com and bar.com 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.foo and com.bar 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 foo employee class, but the bar employee class gets loaded first from the classpath you will get a runtime error, since the bar employee class may not have same structure as the other one.

saugata