Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of the reverse domain name for java package structure

Tags:

java

packages

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?

like image 930
Vaishak Suresh Avatar asked Mar 19 '10 05:03

Vaishak Suresh


People also ask

What is reverse domain name in Java?

Reverse domain name notation (or reverse-DNS) is a naming convention for components, packages, types or file names used by a programming language, system or framework. Reverse-DNS strings are based on registered domain names, with the order of the components reversed for grouping purposes.

Why are domains backwards?

Host names existed before domain names. When domains were bolted on they used the idea of a default domain for each host and that made sense to be on the end. Since people knew the host names and were used to dealing with them, the suffix was more natural since it kept the domain cruft out at the edge.

What is domain package in Java?

Usually domain sub-package is used to place domain objects (or model objects) mainly in projects implemented around MVC pattern, but not only. Model objects could map tables in your DB (if you are using an ORM) or they are just classes that represent the entities involved in your application logic.

What should be the package name in Java?

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 .


2 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.

like image 167
Claudiu Avatar answered Oct 17 '22 23:10

Claudiu


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.

like image 40
Laurence Gonsalves Avatar answered Oct 17 '22 23:10

Laurence Gonsalves