The import keyword

The import statement make available one or all the classes in a package to the current source file.

The import keyword

The import keyword

     

The import statement make available one or all the classes in a package to the current source file. Keywords are basically reserved words which have specific meaning relevant to a compiler. Once a package in a java source file is imported then that class (source file) can use the classes available in the package without specifying the package name to which the class belongs.

Syntax: Here is the syntax to import a package in a class.

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

Due to the rigid naming convention used by java programming language, java compile easily finds the corresponding source file simply by using fully qualified name of the class along with the package name. Here is the syntax for the style of import a package and the class of that package:

java.util.ArrayList a = new java.util.ArrayList ( 25 );

Note: Here are some points that must consider while importing a package.

  • The character "*" must be used in order to import all the class of a package.
  • Avoid use of the character "*" while using one or two class of a package because it can arise ambiguity when multiple package includes classes having same names.
  • Use import statement in the beginning of the source file just to specify that the class or the entire java package can be referred by using the package name.
  • Java versions from J2SE 5.0 to upward can import the static members of a class also.