Java Packages Com and Org

Why do package names often begin with com

It's just a namespace definition to avoid collision of class names. The com.domain.package.Class is an established Java convention wherein the namespace is qualified with the company domain in reverse.

Is something else than com and org used in java?

A package name is defined by the language specification as a succession of valid identifiers, separated by ..

So the convention (for unicity purposes) is to use your domain name but any valid identifier can be used. This is a valid package name too:

é.è.û.¥

Java com.* package namespace

The naming convention for packages is specified in the JLS. Here is the relevant snippet (there's a lot more in the section):

JLS 7.7 Unique Package Names

You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as sun.com. You then reverse this name, component by component, to obtain, in this example, com.sun, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names.

It's also given in Naming Conventions section of Sun's code conventions document:

Packages: The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples: com.sun.eng, com.apple.quicktime.v2, edu.cmu.cs.bovik.cheese


So com. prefix in package names means the same as .com suffix in domain names: "commercial".

How should I name packages if I don't have a domain associated with me?

Use a top-level domain like 'bernard' or something else unique. The important part is that the domain is unique so that you avoid clashes, and not that it starts with a real Internet top-level domain like org or com. E.g.

import java.util.*;
import bernard.myProject.*;
import org.apache.commons.lang.*;

Package names for domains which end in .do

Oracle suggests you add an underscore:

In some cases, the internet domain name may not be a valid package
name. This can occur if the domain name contains a hyphen or other
special character, if the package name begins with a digit or other
character that is illegal to use as the beginning of a Java name, or
if the package name contains a reserved Java keyword, such as "int".
In this event, the suggested convention is to add an underscore. For
example:

hyphenated-name.example.org   org.example.hyphenated_name
example.int int_.example
123name.example.com com.example._123name

Source: https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

What's the convention for java package names without a domain association?

If you are going to be distributing a lot of stuff, I would really suggest getting a domain name. Another alternative however would be to use your e-mail: e.g. bob@gmail.com would become com.gmail.bob. This is less common than using domain names but is still done by some and still ensures uniqueness.

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

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.



Related Topics



Leave a reply



Submit