Basic Java
Unit 5 - Packages
Pratian Technologies (India) Pvt. Ltd.
www.pratian.com
Unit 5 Packages
Topics
Naming Clashes
Packages
Creating Packages in Java
Accessing Package Members
Access Specifiers
Nested Packages
The import statement
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Naming Clashes
class Connection
{
Connect to
Oracle
developed
by team X
public void connectToOracle()
Another Team Z uses utility classes
provided by both teams X & Y
class DatabaseWriter
// Logic of connecting to
Oracle DB
public static void main(String[ ] s)
{
Connection connection = new
Connection();
class Connection
{
Connect to
Sybase
developed
by team Y
public void connectToSybase()
{
//Error
}
How happens
do we solve
What
when
the
problem
of
all the three classes
Name
clashes&?
are
integrated
DatabaseWriter
is executed ?
// Logic of connecting to
Sybase DB
}
}
Copyright 2008 Pratian Technologies
www.pratian.com
connection.
connectToOracle();
Basic Java
Unit 5 Packages
Packages
Package is a collection of related classes and interfaces.
Packages are used to avoid naming conflicts.
They also provide access protection.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Choose an appropriate
name for the package.
package oracleDB;
class Connection
{
Use
the
package
statement at the top of
every source file that
contains the classes or
interfaces.
public void connectToOracle()
{ }
.
}
package cybaseDB;
class Connection
{
public void connectToSybase()
{}
..
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Choose an appropriate
name for the package.
package oracleDB
class Connection
{
Use
the
package
statement at the top of
every source file that
contains the classes or
interfaces.
public void connectToOracle()
{ }
.
}
package cybaseDB;
class Connection
{
Note
public void connectToSybase()
package statement must be the
first line in the source file
{}
..
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Choose an appropriate
name for the package.
package oracleDB
class Connection
{
Use
the
package
statement at the top of
every source file that
contains the classes or
interfaces.
public void connectToOracle()
{ }
.
}
package cybaseDB;
class Connection
{
Note
public void connectToSybase()
There can be only one package
statement in each source file and
it applies to all types in the file.
{}
..
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Package is a library as well !
A physical folder with the same name as the package has
to created
All the .class files belonging to that package has to be
included in that folder.
oracleDB.Connection
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
sybaseDB.Connection
Unit 5 Packages
Classpath Setting
Classpath setting is used by the JVM to locate class files
Class loader looks for a class file in the path mentioned and loads the
bytecodes in memory
The environment variable Classpath needs to be set to the parent
folder of the package folder
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Accessing package members
Referring to the member by its fully qualified name
A class or interface belonging a package needs to be referred to
as package name.class name
class DatabaseWriter
{
public static void main(String[ ] s)
{
oracleDB.Connection conn = new
oracleDB.Connection();
conn. connectToOracle();
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Summarizing steps involved in creating packages
i.
Include package statement
ii.
Create the corresponding physical folder structure
iii.
Set the classpath environment variable
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Access Specifiers - Revisited
public
Any class member declared as public is visible (or accessible) to
any class belonging to any package
protected
Any class member declared as protected is visible (or accessible)
to all classes in the same package as well as to sub classes
belonging to other packages
default
Any class member declared without any of the above is visible (or
accessible) to all classes in the same package only.
private
Any class member declared as private is visible (or accessible)
only within the same class
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Knowledge Check
Include any class that you have written in a package and use the
class in another class.
Note : To run a class that belongs to a package, remember to provide
the fully qualified class name as argument to the java command
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Package Naming Convention
It is important to ensure uniqueness of package names
Since the Internet domain names are unique for a
company, the reversed domain name is used to name a
package.
Ex com.pratian.javatraining for a package javatraining
Package names are written in lowercase to avoid conflict
with the names of classes or interfaces.
Packages in the Java language itself begin with java. or
javax.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Nested Packages - Example
Could be
Cumbersome
Class Customer belongs to
package
com.pratian.pb.model
In another class CustomerDAO,it needs to
be accessed as
com.pratian.pb.model.Customer
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
The import shorthand
The import statement is a convenience syntax used to
access classes that belong to a package
The import statement
makes
it easier
to
must
appear
at the
access package
beginning,
only next to
members
the
package statement
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
The import shorthand
The wild card character
* is used to specify
all classes of the
package
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Nested Packages
A package can be nested inside another package.
A . separated name is used to nest package within packages.
package instruments.western;
public class Guitar implements
instruments.Instrument
{
public void play() { }
}
On compilation of the above code we would get a Guitar.class file,
this file should be included in a directory instruments/western.
C:/myproject/instruments/western/Guitar.class
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
JAR Files
The Java Archive (JAR) file format enables to bundle
multiple files into a single archive file.
Need for JAR files
Compression The JAR format allows compression of files for
efficient storage.
Decreased download time If a library is bundled in a JAR file,
the resources can be downloaded in a single transaction, without
the need for opening a new connection for each file.
Portability - The mechanism for handling JAR files is a standard
part of the Java platform's core API.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating a JAR File
The basic format of the command for creating a JAR
file is:
jar cvf jar-file input-file(s)
The options and arguments used in this command
The c option indicates that you want to create a JAR file.
The f option indicates that you want the output to go to a file
rather than to stdout.
jar-file is the name that you want the resulting JAR file to have.
By convention, JAR filenames are given a .jar extension,
though this is not required.
The input-file(s) argument is a space-separated list of one or
more files that you want to include in your JAR file. The inputfile(s) argument can contain the wildcard * symbol. If any of the
"input-files" are directories, the contents of those directories are
added to the JAR archive recursively.
This command will generate a compressed JAR file
and place it in the current directory.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Viewing contents of a JAR File
The basic format of the command for viewing the contents
of a JAR file is:
jar tvf jar-file
The options and arguments used in this command are:
The t option indicates that you want to view the table of contents
of the JAR file.
The f option indicates that the JAR file whose contents are to be
viewed is specified on the command line.
The jar-file argument is the path and name of the JAR file whose
contents you want to view.
The t and f options can appear in either order, but there must not
be any space between them.
This command will display the JAR file's table of contents
to stdout.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Adding a JAR file to class path
While adding JAR files to the classpath, it is necessary to
add, not just the location, but also the file name of the
JAR file.
E.g. Consider a JAR file called MyLib.jar located in
C:\MyJava\Project\MyLib.jar
set classpath=C:\MyJava\Project\MyLib.jar
And not
set classpath=C:\MyJava\Project\
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Question time
Please try to limit the questions to the topics discussed during the session. Thank you.
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Creating packages involves the following three steps
i.
Include package
statement
ii. Create the corresponding
physical folder structure
iii.
Set the classpath environment variable
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Creating packages involves the following three steps
i.
Include package statement
ii.
Create the corresponding
physical folder structure
iii.
Set the classpath environment variable
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 5 Packages
Creating Packages
Creating packages involves the following three steps
i.
Include package statement
ii.
Create the corresponding physical folder structure
iii.
Set the classpath
environment variable
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java