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

1
2
3
4
5
6
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.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on