0% found this document useful (0 votes)
7 views2 pages

Unit 2

Uploaded by

mahimanoharn
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)
7 views2 pages

Unit 2

Uploaded by

mahimanoharn
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/ 2

UNIT-2

Packages Packages and interfaces are two of the basic components of a Java
program. In general, a Java source file can contain any (or all) of the following four
internal parts:
* A single package statement (optional)
* Any number of import statements (optional)
* A single public class declaration (required)
* Any number of classes private to the package (optional)

Java provides a mechanism for partitioning the class name space into more
manageable chunks. This mechanism is the package. The package is both a naming and a
visibility control mechanism. You can define classes inside a package that are not
accessible by code outside that package. You can also define class members that are only
exposed to other members of the same package.

Defining a Package:

* Creating a package is quite easy: simply include a package command as the first
statement in a Java source file. Any classes declared within that file will belong to the
specified package. The package statement defines a name space in which classes are
stored.

* If you omit the package statement, the class names are put into the default package,
which has no name. While the default package is fine for short, sample programs, it is
inadequate for real applications. Most of the time, you will define a package for your
code.

Creating a Package:
* The general form of the package statement:
package pkg; Here, pkg is the name of the package.

* For example, the following statement creates a package called MyPackage.


package MyPackage;
Java uses file system directories to store packages. For example, the .class files
for any classes you declare to be part of MyPackage must be stored in a directory called
MyPackage. Remember that case is significant, and the directory name must match the
package name exactly.
More than one file can include the same package statement. The package
statement

simply specifies to which package the classes defined in a file belong. It does not exclude
other classes in other files from being part of that same package. You can create a
hierarchy of packages. To do so, simply separate each package name from the one above
it by use of a period.
* The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];

* A package hierarchy must be reflected in the file system of your Java development.
For example, a package declared as package java.awt.image; needs to be stored in
java/awt/image, java\awt\image,
Or
java:awt:image
on your
UNIX, Windows, or Macintosh file system, respectively. Be sure to choose your
package names carefully.
* You cannot rename a package without renaming the directory in which the classes are
stored.

Finding Packages and CLASSPATH:


Java run-time system know where to look for packages that you create? The
answer has two parts.
* First, by default, the Java run-time system uses the current working directory as its
starting point. Thus, if your package is in the current directory, or a subdirectory of the
current directory, it will be found.
* Second, you can specify a directory path or paths by setting the CLASSPATH
environmental variable.
For example, consider the following package specification.
package MyPack;

In order for a program to find MyPack, one of two things must be true. Either the
program is executed from a directory immediately above MyPack, or CLASSPATH
must be set to include the path to MyPack.
The first alternative is the easiest (and doesn‘t require a change to
CLASSPATH), but the second alternative lets your program find MyPack no matter
what directory the program is in.
Create the package directories below your current development directory, put the
.class files into the appropriate directories and then execute the programs from the

You might also like