import Statement
The import statement in Java is used to make classes and interfaces from other packages accessible in the current source file without using their fully qualified names. It improves readability, maintainability, and developer productivity. This is a high-frequency interview topic, closely tied to packages and access control.
What Is the import Statement?
- Allows access to classes from other packages
- Eliminates need for fully qualified names
- Used at compile time
- Improves code clarity
import packageName.ClassName;
Why import Is Needed
Without import:
java.util.ArrayList list = new java.util.ArrayList();
With import:
import java.util.ArrayList;
ArrayList list = new ArrayList();
✔ Cleaner and more readable
Where import Statement Is Written
- After package statement
- Before class definition
package com.example.app;
import java.util.List;
import java.util.ArrayList;
public class Test { }
Types of import Statements
1️⃣ Single-Class Import
Imports one specific class.
import java.util.ArrayList;
- ✔ Most preferred
- ✔ Avoids ambiguity
2️⃣ Wildcard Import (*)
Imports all classes from a package.
import java.util.*;
- ⚠️ Does not import sub-packages
- ⚠️ May reduce readability
3️⃣ Static Import (Preview)
Imports static members of a class.
import static java.lang.Math.sqrt;
double r = sqrt(16);
✔ Used for constants and utility methods
Automatic Import (java.lang)
All classes in java.lang are automatically imported.
String s = "Java";
System.out.println(s);
✔ No import required
import vs Fully Qualified Name
| Aspect | import | Fully Qualified Name |
|---|---|---|
| Readability | High | Low |
| Usage | Preferred | Rare |
| Scope | Whole file | Single usage |
| Performance | Same | Same |
✔ import does not impact performance
Import and Name Conflicts (Interview Favorite)
import java.util.Date;
import java.sql.Date;
❌ Compile-time error: ambiguous reference
Solution 1: Use Fully Qualified Name
java.util.Date d = new java.util.Date();
Solution 2: Import One, Qualify the Other
import java.util.Date;
java.sql.Date d2 = new java.sql.Date();
Multiple Imports in a File
- ✔ Allowed
- ✔ Order does not matter
import java.util.List;
import java.util.Map;
Importing User-Defined Packages
import com.mycompany.util.MathUtil;
✔ Same rules apply as built-in packages
Common Beginner Mistakes
- Forgetting import statement
- Assuming wildcard imports include sub-packages
- Importing unused classes
- Confusing static import with normal import
- Thinking imports increase runtime memory usage
Interview-Ready Answers
Short Answer
The import statement allows access to classes from other packages without using fully qualified names.
Detailed Answer
In Java, the import statement is used to make classes and interfaces from other packages available in the current source file. It improves code readability and is processed at compile time. The java.lang package is imported automatically.
Key Takeaway
import improves readability, not performance. Use specific imports for clarity and avoid wildcard imports unless justified.