Learning Java
Custom Search

Previous Next

Packages and Imports

Packages in Java make it possible to group and organize related classes and interfaces. Java provides a large group of packages for use in developing java applications. You may also create your own packages to make your classes available to others to use.

The classes and interfaces in packages are organized under a directory structure. These items are placed in a particular package by use of the package statement. This package statement must be the first non-comment statement in the program source file. There should be only one package statement in a file even though it can contain multiple classes which will be placed in that package. That same package statement can be used in other files, which would place all classes together using the same package name. A sample of a package statement might be

    package mydir.mysubdir;

This places the contents of this file into the directory mysubdir which is a subdirectory of mydir. That is,

    \mydir\mysubdir

The directory mydir should be placed under your CLASSPATH or it can be specified by using the -classpath parameter of the java command.

To refer to a class in a package, you can use its full name. The following example uses the class called GregorianCalendar to display the current date and time. This class is stored in the package java.util. In order to reference the class below, we refer to it by using its full name java.util.GregorianCalendar.

public class SimpleDate {
	
    public static void main(String args[]) {
	java.util.GregorianCalendar cal 
	    = new java.util.GregorianCalendar();

	System.out.println("Date & Time = " + cal.getTime());
    }
}

Using the full name can be cumbersome and make our code difficult to read. Java provides us with an import statement that allows us to access the classes and interfaces in a package without using the full name. The import statements must be at the top of the file, after any package statement. Using the import statement, our code would change to the following.

import java.util.GregorianCalendar;

public class SimpleDate {
	
    public static void main(String args[]) {
	GregorianCalendar cal = new GregorianCalendar();

	System.out.println("Date & Time = " + cal.getTime());
    }
}

Note how much simpler the second code is to read.

	java.util.GregorianCalendar cal = 
			new java.util.GregorianCalendar();
		vs
	GregorianCalendar cal = new GregorianCalendar();

The import statement could have been either

	import java.util.GregorianCalendar;
		or
	import java.util.*;

The first import statement provides only the class that is needed in this code. The second makes available all classes and interfaces in java.util. The asterisk "*" can be used as a wildcard to represent all classes and interfaces in that package. However, you can not use the * to represent subdirectories. All the subdirectories involved must be specified.

With "import java.util.*", you have access to any class or interface in that package. For example, besides GregorianCalendar, there are the classes TimeZone and Random in this package. The full name would not be necessary for these. However, if you want to use a class that has the same name in 2 different packages, you have to use its full name.

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved