You are on page 1of 4

Chapter 7

JAVA PACKAGES

This lesson will help students to learn various built-in and user-defined packages of
Java.
Your
highlight points during this session should be:
􀀃 Built-in packages
􀀃 User-defined packages
􀀃 Exploring the java.lang package
A package is a set of classes that are stored in a directory, which has the same name
as the package name. Packages enable you to organize the class files provided by
Java. The Java packages are classified into the following two categories:
􀀃 Java Application Programming Interface (API) packages
The Java API consists of various packages, such as java.lang, java.util, java.io, java.awt,
java.net, and java.applet.
􀀃 Java user-defined packages:
The packages that a user creates are called userdefined packages. The user-defined
packages can be imported in any Java program.

The import statement is used to include a Java package in a program. The following
syntax shows how to import a package in a Java program:
import<package_name>.*;
import<package_name>.<class_name>;
In the preceding syntax, the name of a package is followed by an asterisk (*) or by
the name of a class in the package, which you need to import in the program. The dot
operator (.) separates the elements in the syntax statement, such as package_name,
and class name. The following figure shows the structure or the hierarchy of the Java API
packages:
Creating a User-defined Package
Java provides the feature of creating a package to organize the related classes. You
create a user-defined package by using the keyword, package. The package
declaration must be at the beginning of the source file. You can make only one
package declaration in a source file. The following syntax shows how to create a
userdefined
package:
package <package_name>
// Class definition
public class <classname1>
{
// Body of the class.
}
public class <classname2>
{
// Body of the class.
}
The file containing the package is saved as .java file. After compiling the source code,
the .class file is created that is stored in the directory having the same name as the
package name. To create a user-defined package, perform the following steps:
1. Create a source file containing the package definition.
2. Create a folder having the same name as package name and save the source
file within the folder.
3. Compile the source file.
Importing a User-defined Package
You can include a user-defined package using the import statement. The following
syntax shows how to include the user-defined package, empDetails from the app
directory in a program:
import app.empDetails.Employee
In the preceding syntax, the Employee class is imported from the empDetails
package. You can use the following syntax to implement the user-defined package,
empDetails:
import app.empDetails.Employee;
public class Director extends Employee
{
// Body of the class.
}
When you compile the file containing the package statement, the .class file is saved in
a directory with the name of the package. For example, the Employee.class file will
exist in the following directory:
<DIR>\app\empDetails

In the preceding statement, DIR is the path to the directory where the file is saved.
When you compile the program using the –d option, the compiler creates the package
directory and moves the compiled class file to it. You can use the following syntax to
compile the program:
javac –d .Employee.java

Ex
package packtest;
public class PackageTest
{
String custname;
int acct_no;
int cust_id;
//Passing values in the constructor 'SamplePack'.
public PackageTest(String n1,int r1,int r2)
{
custname=n1;
acct_no=r1;
cust_id=r2;
}
public void display()
{
System.out.println(“CUSTOMER INFORMATION");
System.out.println(" "+"Name of the Customer: "+custname);
System.out.println(" "+"Account Number :"+acct_no);
System.out.println(" "+"Customer ID:" +cust_id);
}
}
Compile the preceding code snippet using the following syntax:
javac –d c:\java SamplePack.java
You can use the following code to implement the user-defined package BankDet in a
program:
import packtest.PackageTest;
public class Bank
{
public static void main(String args[])
{
//Creating instance of the class 'SamplePack'.
PackageTest myobj=new PackageTest ("John Smith",1001,1002);
myobj.display();
}
}
Compile the preceding code by using the following syntax:
javac Bank.java
Execute the preceding code by using the following syntax:
java Bank

You might also like