A package is a group of classes, and is used to organize your source code. The keyword package at the start of a .java file declares the package name for the classes within the file. Multiple .java files may declare themselves a part of the same package.

Package names are usually implemented in dotted notation, and simulate a hierarchical structure. Packages starting with java. are a code part of the Java environment (javax. is an exception, for the Swing components ). Package names are also used to enable the use of two classes with the same name, as long as their package names differ .

By convention, companies creating their own packages usually use their internet domain names in reverse order, along with a description of the package itself. For example, would be the package created by www. for their SQL JDBC driver. might be the package the created for its security classes.

If you do not specify a package name in your .java file, then your classes are considered a part of the default package and have no package name. This is a very poor practice for anything but small test programs and classes. For example:


public class Crypt {
boolean setKey(String Key) {};
String getKey(){ };

Using the import keyword, you are specifying which classes or packages you want to be able to refer to be their simple names. For example, without an import statement, the class above would be called . But, by including import in the program, the name would simply be Crypt . You can also use wildcards when declaring an import statement, to simply access to all classes at a specific level in the package, such as import*; to import all the openroad security classes.

In order for predefined classes to be used from their packages, you must also set up your CLASSPATH environment variable correctly. The CLASSPATH environment variable defines where the java compiler and the JRE are to look for user packages. The CLASSPATH value contains a list of directory names and/or jar file names.

In order to use the above class properly as a reusable class, you should do the following:

  • Create the class and specify the proper package name.

  • Compile the .java file, and create a .class file.

  • Copy the .class file to a folder structure that mimics the package name. For example:

  • Set the CLASSPATH environment variable to point to the folder containing the root of the folder structure. Also include the . Folder. For example:

     SET CLASSPATH=C:\MyJavaClasses; 
  • Write a new program in a separate folder where the Crypt files do not exist. The code should look similar to the following:

    public class MyApp {
    public static void main(String[] args)
    Crypt cr = new Crypt();

  • The new application should compile and run cleanly, even though Crypt.class is not in the current folder.


    If you alter the CLASSPATH variable, you should be sure to include the . path (current directory) in its definition. Otherwise, classes in the current directory will not be identified by the JRE tools.

OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: