0% found this document useful (0 votes)
32 views31 pages

Prof Sanket J Shah: Prepared By: Alpha College of Enginering & Technology

The document discusses Java packages. It defines what packages are and their purposes, which include preventing naming conflicts, providing access protection, and categorizing classes and interfaces to improve reusability. It describes built-in Java packages and how to define user-defined packages. It explains how to create packages, access packages from other packages using import statements or fully qualified names, and set the CLASSPATH environment variable. It also discusses static imports and the difference between regular imports and static imports.

Uploaded by

rsadsad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views31 pages

Prof Sanket J Shah: Prepared By: Alpha College of Enginering & Technology

The document discusses Java packages. It defines what packages are and their purposes, which include preventing naming conflicts, providing access protection, and categorizing classes and interfaces to improve reusability. It describes built-in Java packages and how to define user-defined packages. It explains how to create packages, access packages from other packages using import statements or fully qualified names, and set the CLASSPATH environment variable. It also discusses static imports and the difference between regular imports and static imports.

Uploaded by

rsadsad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CHAPTER 5

PACKAGE

Prepared By:
Prof Sanket J Shah
Alpha College of Enginering & Technology
Packages
• A java package is a group of similar types of
classes, interfaces and sub-packages.

• Packages are used to prevent naming conflicts


and provides access protection.

• It is also used to categorize the classes and


interfaces so that they can be easily maintained.

• More importantly, it helps improve re-usability.


• Package can be categorized in two form : built-in
package and user-defined package.

• built-in packages : Existing Java package


such as java.lang, java.util, java.io, java.net,
java.awt

• User-defined-package : Java package


created by user to categorized classes and
interface

• Programmers can define their own packages to


bundle group of classes,interfaces etc.
• We can also categorize the package further by
using concept of subpackage. Package inside the
package is called the subpackage.
Creating Package

• To create a package, package statement


followed by the name of the package.

• The package statement should be the first line in


the source file. There can be only one package
statement in each source file.

• If a package statement is not used then the class,


interfaces etc. will be put into an unnamed
package.
How to Access Package from another
package?

There are three ways to access the package from


outside the package.

• import package.*;
• import package.classname;
• fully qualified name.
1. Using Packagename.*;

• If you use package.* then all the classes and


interfaces of this package will be accessible but
not subpackages.

• The import keyword is used to make the classes


and interface of another package accessible to
the current package.
2. Using Packagename.classname;

• If you import package.classname then only


declared class of this package will be accessible.
3. Using fully qualified name
•If you use fully qualified name then only declared
class of this package will be accessible. Now there
is no need to import. But you need to use fully
qualified name every time when you are accessing
the class or interface.

•It is generally used when two packages have same


class name e.g. java.util and java.sql packages
contain Date class.
Example 1

Note: Creating class in the package named Relational


Note: Class where package is being used
Note: Location of both the java files
Compiling the Package

Note: See the Background, Folder named Relational is created by


Compiling the package. Folder contains Compare.class file
Run the package
Use the created package

Note: This program uses the getMax method of Compare class


Example 2
Three classes in one package
1st: One.java
2nd: Two.java
3rd: Three.java
Using all three classes of package
Output
Example 3 (using Fully Qualified name)
Output
Access Control
CLASSPATH
• CLASSPATH is an environment variable (i.e.,
global variables of the operating system available to
all the processes) needed for the Java compiler and
runtime to locate the Java packages used in a Java
program.

• This is similar to another environment variable


PATH, which is used by the CMD shell to find the
executable programs.

• To check the current setting of the CLASSPATH,


issue the following command:

> SET CLASSPATH


CLASSPATH can be set in one of the following
ways:

• CLASSPATH can be set permanently in the


environment: In Windows, choose control panel ⇒
System ⇒ Advanced ⇒ Environment Variables ⇒
choose "System Variables" (for all the users) or
"User Variables" (only the currently login user) ⇒
choose "Edit" (if CLASSPATH already exists) or
"New" ⇒ Enter "CLASSPATH" as the variable
name ⇒ Enter the required directories
To set the CLASSPATH variable −

In Windows →
set CLASSPATH = C:\users\jack\java\classes

In UNIX →
% CLASSPATH = /home/jack/java/classes; export
CLASSPATH
Static Import
• The static import feature of Java 5 facilitate the
java programmer to access any static member of
a class directly. There is no need to qualify it by
the class name.

• Advantage of static import:


Less coding is required if you have access any
static member of a class oftenly.

• Disadvantage of static import:


If you overuse the static import feature, it makes
the program unreadable and unmaintainable.
What is the difference between import and
static import?

The import allows the java programmer to access


classes of a package without package
qualification whereas the static import feature
allows to access the static members of a class
without the class qualification. The import
provides accessibility to classes and interface
whereas static import provides accessibility to
static members of the class.
Example : Static import
Output
Thank You

You might also like