Java_Packages
Java_Packages
PACKAGES
Defining a Package
Finding Packages and CLASSPATH
Access Protection
Importing Packages
Packages:
3
Basics:
• Packages are containers for classes that are used to keep the class name space
compartmentalized. (Is used to group related classes. Think of it as a folder
in a file directory)
• Eg: A package allows you to create a class named List, which you can store in
your own package without concern that it will collide with some other class
named List stored elsewhere. (packages are used to avoid name conflicts)
• Packages are stored in a hierarchical manner and are explicitly imported into
new class definitions.
• A Java’s mechanism for partitioning the class name space into more
manageable chunks.
Packages:
4
Basics: ...
• The package is both a naming and a visibility control
mechanism.
• One can define classes inside a package that are not accessible
by code outside that package.
✓Second, you can specify a directory path or paths by setting the CLASSPATH
environmental variable.
✓Third, you can use the -classpath option with java and javac to specify the
path to your classes.
Packages:
14
Finding Packages and CLASSPATH: ...
• Eg: package MyPack; In order for a program to find MyPack, one of three things
must be true.
✓ Either the program can be executed from a directory immediately above MyPack, or
✓ the CLASSPATH must be set to include the path to MyPack, or
✓ the -classpath option must specify the path to MyPack when the program is run via
java.
• When the second two options are used, the class path must not include MyPack, itself. It must simply
specify the path to MyPack. For example, in a Windows environment, if the path to MyPack is
C:\MyPrograms\Java\MyPack Then the class path to MyPack is
C:\MyPrograms\Java\MyPack
Packages:
•A Short Package Example 15
Access Protection:
• Packages add another dimension to access control. Java provides many
levels of protection to allow fine-grained control over the visibility of
variables and methods within classes, subclasses, and packages.
• For this reason, Java includes the import statement to bring certain classes,
or entire packages, into visibility. Once imported, a class can be referred to
directly, using only its name
Packages:
25
Importing Packages: ...
• The import statement is a convenience to the programmer and is not
technically needed to write a complete Java program. If you are going to
refer to a few dozen classes in your application, however, the import
statement will save a lot of typing.
• In a Java source file, import statements occur immediately following the
package statement (if it exists) and before any class definitions. The
general form of the import statement:
import pkg1[.pkg2].(classname|*);
• Here, pkg1 is the name of a top-level package, and pkg2 is the name of a
subordinate package inside the outer package separated by a dot (.).
Packages:
26
• There is no practical limit on the depth of a package hierarchy, except that
imposed by the file system. Finally, you specify either an explicit classname
or a star (*), which indicates that the Java compiler should import the entire
package.
• This code fragment shows both forms in use:
import java.util.Date;
import java.io.*;
• CAUTION The star form may increase compilation time specially if you
import several large packages. For this reason it is a good idea to explicitly
name the classes that you want to use rather than importing whole packages.
However, the star form has absolutely no effect on the run-time performance
or size of your classes.
Packages:
27
Importing Packages: ...
• All of the standard Java classes included with Java are stored in a package
called java.
• The basic language functions are stored in a package inside of the java
package called java.lang
• Normally, you have to import every package or class that you want to use,
but since Java is useless without much of the functionality in java.lang, it
is implicitly imported by the compiler for all programs. This is equivalent
to the following line being at the top of all of your programs:
import java.lang.*;
• If a class with the same name exists in two different packages that you
import using the star form, the compiler will remain silent, unless you try
to use one of the classes. In that case, you will get a compile-time error
and have to explicitly name the class specifying its package.
Packages:
28
Importing Packages: ...
• It must be emphasized that the import statement is optional. Any place you
use a class name, you can use its fully qualified name, which includes its full
package hierarchy.
❑ The only time we need to pay attention to packages is when we have a name conflict.
❑ For example both, java.util and java.sql packages have a class named Date. So if we
import both packages in program as follows:
❑ The compiler will not be able to figure out which Date class do we want. This problem
can be solved by using a specific import statement:
Packages:
Illustration of user-defined packages: 32
Creating our first package: File name – ClassOne.java Creating our second package: File name – ClassTwo.java