import keyword in java
The ” import
” keyword makes one class or all classes in a specified package visible in the current Java source file.
Imported classes can be referenced without the use of fully−qualified class names.
We can either import all the classes from a package using “*
” wildcard or
We can import specific class from a package using exact class name in the import.
Example:
import java.net.*;
import java.io.File;
In the first import, we are importing all the classes under java.net package.
In the second import, we are importing only “File” class from java.io package.
The import statements must be placed on top of the source file, just after the package statement.
Example
- package com.kb;
- import java.io.File;
- public class ImportExample{
- //variables and methods goes here
- }
package com.kb; import java.io.File; public class ImportExample{ //variables and methods goes here }
Note:
Its good practice to use only specific import statements (rather than ‘*’) to avoid ambiguity when multiple packages contain classes of the same name.