1

Why does java recommend having TLD in the beginning of the package names i.e., why does it have to be

package com.example.pack;

It can as well be

package pack.example.com;

Just wondering what might be the reason behind deciding to put the TLD in the beginning.

Bajji
  • 2,243
  • 2
  • 22
  • 35

7 Answers7

3

This convention is documented here, it is proven and works for quite a lot of things.

The universe is com and can be SG, ORG or any TLD :).

Followed by your domain example.

Followed by your project pack.

Followed by your layer web | ejb | data.

com.example.pack.web
com.example.pack.ejb
com.example.pack.data
com.example.project2.web
com.example.project2.ejb
com.example.project2.data
com.singtel.projecta.web
com.singtel.projectb.data
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • I'd upvote apart form the `The universe is com` sentence, there are plenty of other commonly used top level package names. e.g. org or net – vickirk May 04 '12 at 10:20
3

The package correspond to directories in the file system. pack.example.com and pack.example2.net would both be in the same directory, even if the url suggest they are not related, but with com.example.pack and net.example2.pack, related packages are in the same directory.

Pablo
  • 3,655
  • 2
  • 30
  • 44
0

It's just the common convention to go from the more general to the more specific, and not vice versa.

In the same vein, we write:

  • package_name.class_name (and not class_name.package_name);
  • object.member;
  • outer_class.inner_class;

and so on.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Java package name should always read from least specific to most specific. Therefore it should always start with domain name first.

Vishal
  • 2,711
  • 7
  • 33
  • 42
0

One good reason for it is that the classname is prefixed rather than suffixed with the package name -- so you're moving in the same direction after each dot. Also, package names map to directory names, which also force the broadest-first order.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

In a tree, the trunk is the main part the leaf is the less general part.

A package name is a tree.

Think of a class search in all classes in your classpath for one written by you. You would start com.* com.example.* etc, etc. Till you find your one.

Víctor Romero
  • 5,107
  • 2
  • 22
  • 32
0

The main motive is to avoid namespace collisions by organizing classes and resources hierarchically, like a filesystem. It would be awkward referencing a text file as MyDoc.txt\My Documents\C:. Using your TLD name, reversed, organizes things more naturally from more general to more specific.

Go Dan
  • 15,194
  • 6
  • 41
  • 65