package : Java Glossary
home P words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-02-12 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
CurrCon neededThe CurrCon Java Applet displays prices on this web page converted with today's exchange rates into your local international currency, e.g. Euros, US dollars, Canadian dollars, British Pounds, Indian Rupees… CurrCon requires Java 1.1 or later, preferably 1.6.0_06 . If you can’t see the prices, of you if just want to learn more about CurrCon, click here for help.
package
If you have a jar or more than one class, assign each class to a package! If you ignore this rule of thumb, all manner of strange and unexpected things will happen to you, including the dreaded NoClassDefFoundError. The most important thing to know about packages is that you can leave them out only for tiny experiments when all your code is in the nameless default package. As soon as you put any of your code into named packages, all of it has to be put into named packages. My rule of thumb is any code I plan to keep more than 24 hours gets put into packages right off the bat. Starting with Java 1.4, you can’t import a class unless it belongs to some named package.

You can’t use classes in the default package from a named package. So except for tiny experiments, always put your classes in a named package of the form com.mindprod.xxx or org.hans.xxx i.e. your domain name backwards, all lower case. If you don’t have one, you can buy a domain for about $7.00 USD a year. You don’t need a website. Failing that, borrow a branch of dyndns.org or other domain with the owner’s permission.

When a source file gets too large, you can split it up, and still have it behave much as if it were one file by declaring all the parts as belonging to the same package. In C++ terminology, all classes in the same package are friends of each other. You just add a line such package com.mindprod.business; at the top of each source *.java file. You use your website name backwards to ensure global uniqueness. The more qualification you have specified in your CLASSPATH the less qualification you are allowed to specify in your import statements. There is no redundancy permitted. It is not enough to simply have your class files on the classpath. They must be in particular locations or inside jars on the CLASSPATH with just the right embedded path information. If you can’t understand my handwaving, try reading

.

Sun tends to put the core classes in the java.* package, the slightly more peripheral classes such as Swing and JavaMail in javax.* and the classes they don’t want users invoking directly in com.sun.*. The core distribution JDK contains all three types, though it excludes some of the javax and com.sun classes.

Traditionally packages have all lower case names, and like all Java names they are case sensitive. In contrast, Sun coding standards require classes to begin with a capital letter and ordinary variables and methods with a lower case letter. Embedded words are capitalised.

According to Sun’s Naming Conventions, you should choose your package name to match your website. e.g. com.mindprod or ca.mindprod with the TLD first. This ensures globally unique package naming.

If you can’t afford to register your domain, pick an obscure one, and check if it is already registered by someone else. If not, you can squat on it, with the possibility somebody might take it from you. Alternatively, use a domain with an illegal TLD (Top Level Domain) e.g. mindprod.programs or roedy.green so your package would be programs.mindprod.xxx or green.roedy.xxx.

When you decide to bring your existing work into alignment with this naming convention, make sure you update:

It might be a good time to tidy your imports while you are at it.

Uniqueness

In theory you can use the same class name so long as they are in different packages. Don't do this this! You will not only confuse the compiler, especially when you want to use both classes in the the same program, you will also confuse people trying to understand your program. Sun make this gaffe using the name List for both an interface and a Component. It causes no end of confusion. When you do this you need to fully qualify all references in a program, which looks ugly and makes the code fragile since some programmer or an IDE will sooner or later remove the necessary qualification.

Learning More

Sun’s Javadoc on the all packages class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 38,686. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/package.html J:\mindprod\jgloss\package.html