package : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)
The 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 an up-to-date browser and Java version 1.8, preferably 1.8.0_131. If you can’t see the prices in your local currency, Troubleshoot. Use Firefox for best results.

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 (Java Development Kit) 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 Oracle’s Naming Conventions, you should choose your package name to match your website, e. g. com.mindprod or ca.mindprod with the TLD (Top Level Domain) 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 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 same program, you will also confuse people trying to understand your program. Sun made this gaffe using the name List for both an interface: java.util.List and a Component: java.awt.Listawt.List. 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 (Integrated Development Environment) will sooner or later remove the necessary qualification.

Example

Here is how you might put an interface and two implementing classes into the same package:

Learning More


This page is posted
on the web at:

http://mindprod.com/jgloss/package.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\package.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.226.104.30]
You are visitor number