How to Create a Java Package

Organize Java Classes by Using a Package

© Mark Alexander Bain

Jan 22, 2009
Creating Java Packages, Mark Alexander Bain
When using any object oriented language like Java the programmer will create classes - lots of them. A programmer can organize these classes by introducing Java packages.

Java is an object oriented programming language and any Java programmer will quickly build a large number of different classes for use in each application that they develop.

Initially it may be tempting to store the classes in the same directory as the end application, but as the complexity applications grow so will the number of classes. The answer is to organize the classes into packages.

The Java Package

A Java package is simply a directory that contains one or more class files. It is, therefore, just a matter of:

  • creating a folder
  • moving the class file to the new folder (or creating them there)
  • calling the package from any applications

Of course, nothing is ever quite that simple.

Creating a Java Package

The first step is (rather obviously) to create the directory for the package. This can go in one of two places:

  • in the same folder as the application that will be using it
  • in a location listed in the system CLASSPATH environmental variable

So, for example, if CLASSPATH contains C:\Java\Packages then the new package can be created there, for example C:\Java\Packages\suite101, and once the directory has been created then classes can be added to it.

The first line of a class in a package is essential - it must contain the name of of the package (i.e. the directory in which the class resides):

package suite101;

No code is allowed above the package statement, but after that critical line the class can be defined as normal. For example it can call any packages that it requires:

import java.util.* ;
import java.text.* ;

In this example the class contains a single method which returns today's date formatted for the current locale:

public class formatted_date {
public static String today () {
Date mydate = new Date();
String dateString = DateFormat.getDateInstance().format(mydate);
return dateString;
}
}

As always the code must be saved to a text file with the same name as the class - in this case that's C:\Java\Packages\suite101\formatted_date.java.

Using a Java Package

Packages are loaded into new applications by using the import method as shown in the above example where the following line was used:

import java.util.* ;

This is a statement recognized by many Java programmers and means "load all of the the classes stored in the the java.util package". Custom packages are loaded into a Java application exactly the same way:

import suite101.* ;

This will load any classes found in the suite101 directory (the location of which is defined by the CLASSPATH environmental variable). The formatted_date class can now be used in the class for the new application:

public class use_packages {
public static void main(String args[]) {
formatted_date newday = new formatted_date();
System.out.println(newday.today());
}
}

The application code must be saved to a file with the same name as its class, but this file may be placed in any location on the computer (for example C:\Java\Apps\creating_packages\use_packages.java) and this can be compiled and run as required.

Summary

Java packages are used to group and organize classes:

  • a packages is a folder (or directory) on a computer
  • the location of packages are defined by using the CLASSPATH environmental variable
  • the package may contain one or more classes
  • each class in the package must have the following as its first line:
    package <package name>;
  • The classes in the package are loaded into a Java application by using:
    import <package name>.* ;

Giving the Java programmer the ability to create and to organize classes in the most efficient ways possible.


The copyright of the article How to Create a Java Package in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Java Package in print or online must be granted by the author in writing.


Creating Java Packages, Mark Alexander Bain
Packages Contain Classes, Mark Alexander Bain
The CLASSPATH Defines where Packages can be Stored, Mark Alexander Bain
The Packages are Used in Java Applications, Mark Alexander Bain
The End Result is a Java Application, Mark Alexander Bain


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Sep 3, 2009 7:20 AM
Guest :
great article ... thanks :-)
Oct 8, 2009 2:10 PM
Guest :
very good and concise!Thanks:-)
Oct 12, 2009 12:10 PM
Guest :
This really is a very useful post
Oct 27, 2009 5:48 AM
Guest :
Thank you.Very good article
4 Comments