In Java, the import statement is used to bring certain classes or the entire packages, into visibility. As soon as imported, a class can be referred to directly by using only its name.
The import statement is a convenience to the programmer and is not technically needed to write complete Java program. If you are going to refer to some few dozen classes into your application, the import statement will save a lot of time and typing also.
In a Java source file, the import statements occur immediately following the package statement (if exists) and before any class definitions.
Below is the general form of the import statement :
import pkg1[.pkg2].(classname|*);
Here, pkg1 is the name of top-level package, and pkg2 is the name of subordinate package inside outer package separated by dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Now, you specify either an explicit classname or a star (*), indicates that the Java compiler should import the full package. Below code fragment shows both forms in use :
import java.util.Date; import java.io.*;
All the basic Java classes included with Java are stored in a package called java. The fundamental language functions are stored in a package inside of the java package i.e., java.lang. Usually, you have to import every package or class which you want to use, but since Java is useless without a lot of the functionality in java.lang package, it is implicitly imported by the compiler for all the programs. This is equivalent to the below line being at the top of all of your programs :
import java.lang.*;
If a class with the same name exists in the two different packages that you import using the star form, the compiler will remain silent, unless you attempt to use one of the classes. In that case, you will get a error i.e., compile-time error, and have to explicitly name the class determining its package.
It must be noted that the import statement is optional. At any place where you use a class name, you can use its fully qualified name, that includes its full package hierarchy. For example, here the below given code 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 { }
Here in this version, Date is fully-qualified.
As shown in the table (in the previous chapter), when a package is imported, only those items in the package declared as public will be accessible to non-subclasses in the importing code. For example, if you want the class Balance of the package named DemoPackage shown earlier to be accessible as a standalone class for the general use outside of the DemoPackage, then you will need to declare it as public and put it into its own file, as shown here in this example :
Here is an example program on import statement in Java:
/* Java Program Example - Java Import Packages */ package DemoPackage; /* No, the class Amount, its constructor, and its * method named display() are public. It means that * they can be used by non-subclass code outside * their package. */ public class Amount { String name; double bal; public Amount(String nm, double bl) { name - nm; bal = bl; } public void display() { System.out.println(name + " : $" + bal); } }
As you can see here that the Amount class is now public. And, its constructor and its display() method are also public, too. It means that they can be accessed by any type of code outside the package DemoPackage. For example, here the TestAmount imports DemoPackage and is then able to make use of the Amount class :
import DemoPackage.*; class TestAmount { public static void main(String args[]) { /* because Amount is public. you may use * Amount class and call its constructor */ Amount dis = new Amount("Alok Singh", 990.99); dis.display(); // you may also call the display() } }
As an experiment, remove the public specifier from the Amount class and try compiling the TestAmount. Errors will result as explained.
Here are some list of example programs that uses import statement:
Java Programming Online Test
Tools
Calculator
Quick Links