Java: About Packages

This is a grouping of related classes and/or interfaces into a library.


How to Import a Pacakge

If you need a class in a package, then the package must be imported into the java code have the following at the top of the program:
import package_name



How to reference a class not in an imported package

If not imported then the call to the class must explicitly reference the pacakge (e.g. the Graphics class in the package java.awt would be called as java.awt.Graphics).

Create your own package called MyPack
  1. The first line of each of the source files:
    pacakge MyPack
  2. Compile each source file using the -d option to indicate where to place the directory structure for the new classes.
     javac -d classes_dir file1.java
  3. After compilation a subdirectory in the classes_dir directory called MyPack will be created that contains the files file1.class, etc.
  4. Now you can import this package import MyPack



Some Java Packages
  1. java.applet
    This package includes classes for use in an applet.
  2. java.awt
    This is the Abstract Windowing Toolkit package (awt) and contains graphical GUI components usch as panels, graphics, menus, buttons, etc.
  3. java.io
    This package contains classes to perform input and output operations to files.
  4. java.lang
    This is the basic Java package that contains the most basic class Object and also contains Thread, Exception, System, Integer, Float, Math, String classes.
  5. java.net
    This package supports TCP/IP network protocols.



IMPORTANT All applets are subclasses of the class Applet which is part of the package java.applet.
public class HelloAgainApplet extends java.applet.Applet{
}