5 Packages
5 Packages
together
Introduction
• A way to achieve reusability in Java
• A concept similar to ‘class libraries’ in other
languages.
• Packages are Java’s way of grouping a variety
of classes and/or interfaces together.
• Grouping is usually done according to
functionality.
• Packages act as ‘containers’ for classes.
Benefits of using packages
• The classes contained in the packages of other programs
can be easily reused.
• Two classes in two different packages can have the same
name. They may be referred by their fully qualified name,
comprising the package name and the class name.
• Packages provide a way to ‘hide’ classes thus preventing
other programs or packages from accessing classes that
are meant for internal use only.
• Packages provide a way for separating ‘design’ from
‘coding’.
Classifications of Java packages
• Java packages are classified into two types:
– Java API packages
– User defined packages
JAVA API PACKAGES
• Java API(Application Programming Interface)
provides a large number of classes grouped
into different packages according to
functionality.
Frequently used API packages
STAND ALONE APPLICATION BASED PACKAGE
This statement uses a fully qualified class name Math to invoke the method pow().
• java.awt.Point pts[ ]
This statement declares an array of Point type objects using the fully
qualified class name.
CREATING PACKAGES
• First declare the name of the package using the package keyword followed by
a package name.
• This must be the first statement in a Java source file(except for comments
and white spaces)
• eg:
package firstPackage; //package declaration
public class FirstClass //class definition
{
body of class
}
• Save this as a file called FirstClass.java, located in a directory named
firstPackage.
• When the source file is compiled, Java will create a .class file and store it in
the same directory.
Creating our own package involves the following steps:
1. Declare the package at the beginning of a file using the form
package packagename;
2. Define the class that is to be put in the package and declare it public.
3. Create a subdirectory under the directory where the main source files
are stored.
4. Store the listing as the classname.java file in the subdirectory created.
5. Compile the file. This creates .class file in the subdirectory.
• A java package file can have more than one class definitions. In such
cases, only one of the classes may be declared public and that class name
with .java extension is the source file name.
• When a source file with more than one class definition is compiled, Java
creates independent .class files for those classes.
Using a package
• package package1;
public class ClassA
{
public void displayA()
{
System.out.println(“Class A”);
}
}
• This source file should be named classA.java and stored in the subdirectory package1.
Compile this java file. The resultant ClassA.class will be stored in the same subdirectory.
• import package1.ClassA;
class PackageTest1
{
public static void main(String args[ ])
{
ClassA objectA = new ClassA();
objectA.displayA();
}
}
• This source file should be saved as PackageTest1.java and then compiled. The source file and
the compiled file would be saved in the directory of which package1 was a subdirectory.