Packages provide a mechanism for grouping together related classes. Organizing your code into packages is somewhat similar to organizing your files in directories. Files that are related, for example assignments for a single class, are grouped together into a single directory. Classes that have a related function are grouped together into a single class. The Java platform provides several built-in packages including the following:
com.ibm.XXX.YYY. The package to which a class belongs is specified as the first line of the java file. The syntax is simply package followed by the name:
Suppose a class C in package X wanted to create an instance of the class cs112.util.FileOutput. Using the fully qualified name would look as follows:
cs112.util.FileOutput fo = new cs112.util.FileOutput();
To import the class, you would add the following line at the top of C.java:
In this case, you could then create an instance of FileOutput without using cs112.util as part of the type.
To import the entire package, you would add the following line at the top of C.java:
In this case, you could then use any class in the cs112.util package without using the fully qualified name.