78

It's quite often to see com.* package namespace. What does 'com' mean? Thanks.

Stan
  • 37,207
  • 50
  • 124
  • 185

6 Answers6

75

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

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 6
    An interesting bit about this was the original Java language specification suggested that the top-level domain part be written in upper case, such as `COM.stackoverflow.api` or whatever. Nobody actually did this, so they changed the unique package name recommendation as above. – Greg Hewgill Apr 18 '10 at 09:41
  • 2
    @Greg: Nice info! That is indeed the case back in first edition! http://java.sun.com/docs/books/jls/first_edition/html/6.doc.html#9184 – polygenelubricants Apr 18 '10 at 10:00
  • 10
    Isn't this a bad practice? tying your code to a domain name? what if we become a non profit and change to .org. – Paul Wade Feb 27 '15 at 18:36
36

When it created Java, Sun established a convention that package names should be constructed starting with the reversed domain names of the company or individual creating the package. Because DNS guarantees that domain names are unique (i.e. given to only one organization or person), this avoids duplication.

So Java packages by Microsoft have names starting with com.microsoft, those from Sun are com.sun and so on.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
10

Java packages other than the standard libraries are typically named in reverse order of the domain of the vendor, e.g. Microsoft uses com.microsoft, Apache may use org.apache, etc. (See this link for details on naming conventions in Java.) This is to prevent naming conflicts. So the com is just part of the domain name, it has no special meaning to Java.

There is no requirement that you name your packages that way, it's just a very good idea for any package that may remotely end up being used outside your organization.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

com is derived from commercial. Like org is derived from organization.

bruno conde
  • 47,767
  • 15
  • 98
  • 117
0

Package represent a directory that contain related group of class and interface. package are two type. 1. Built in package. 2. user define package. note. a package can contain sub package. eg. java.awt(here awt is a package); java.awt.event(here event is a sub package which present in java.awt)

-1

If you didn't do that before - download some source code of some Java application (jEdit for example) and look and the files structure in the sources. It will clarify everything a bit probably. in jEdit there are several different packages, so you'll see how everything works.
And by the way - if you never read anyone's code - go and do it ASAP!! Even if you don't understand everything or just a small parts - do it as it's a great school of good programming practices!!!

trzewiczek
  • 1,791
  • 1
  • 13
  • 15
  • 6
    I would be careful with learning by reading other's code. You might not know whether the *other* is doing it correctly. – whiskeysierra Apr 18 '10 at 10:32