Import Package In Java Program Explain With Example

How To Import Packages In Java Program, Import Package In Java Program Explain With Example

In This Topic & Blog Having Any Query Then Post your Comment and Suggestion Below

Importing Packages
Given that packages exist and are a good mechanism for compartmentalizing diverse classes
from each other, it is easy to see why all of the built-in Java classes are stored in packages.
There are no core Java classes in the unnamed default package; all of the standard classes
are stored in some named package. Since classes within packages must be fully qualified
with their package name or names, it could become tedious to type in the long dot-separated
package path name for every class you want to use. 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. 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. This is 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 ( ). 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.*;
CAUTIONAUTION
The star form may increase compilation time—especially 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.
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.
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. For
example, this fragment uses an import statement:

import java.util.*;
class MyDate extends Date {
}
The same example without the
import
statement looks like this:
class MyDate extends java.util.Date {
}
In this version,
Date
is fully-qualified.
As shown in Table 9-1, when a package is imported, only those items within the package
declared as
public
will be available to non-subclasses in the importing code. For example,
if you want the
Balance
class of the package
MyPack
shown earlier to be available as a
stand-alone class for general use outside of
MyPack
, then you will need to declare it as
public
and put it into its own file, as shown here:
package MyPack;
/* Now, the Balance class, its constructor, and its
show() method are public.  This means that they can
be used by non-subclass code outside their package.
*/
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
}
public void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
As you can see, the
Balance
class is now
public
. Also, its constructor and its
show( )
method are
public
, too. This means that they can be accessed by any type of code outside
the
MyPack
package. For example, here
TestBalance
imports
MyPack
and is then able to
make use of the
Balance
class:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
/* Because Balance is public, you may use Balance
class and call its constructor. */
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show(); // you may also call show()
}
}
As an experiment, remove the
public
specifier from the
Balance
class and then try
compiling
TestBalance
.
As explained, errors will result.

Labels: