Java Tutorial Series: This lesson is part of our comprehensive Complete Java Tutorial: Learn Java from Beginner to Advanced.
This guide explains the topic from beginner level through practical Java development, common mistakes, debugging, design considerations, examples, and practice. The goal is to understand not only the syntax, but when and why the feature should be used in a real Java project.
What Does import Do?
import lets source code use an accessible type or static member by a simpler name instead of repeatedly writing its fully qualified name.
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();
Fully Qualified Names
A fully qualified name includes the package and type. It can be used directly without an import.
java.util.ArrayList<String> names = new java.util.ArrayList<>();
Wildcard Imports
A wildcard can make accessible types in a package available by simple name. It does not include subpackages.
import java.util.*;
Static Imports
Static imports allow selected static members to be referenced without the class qualifier.
import static java.lang.Math.PI;
System.out.println(PI);
Name Conflicts
If two packages provide the same simple type name, using both simple names can become ambiguous. A fully qualified name can resolve the conflict.
java.util.Date a = new java.util.Date();
java.sql.Date b = new java.sql.Date(System.currentTimeMillis());
java.lang
Classes in java.lang are automatically available by simple name, which is why String, System, Math, and Object normally need no explicit import.
Import vs Dependency
An import does not install a library or add a dependency. A third-party library must be present through the classpath/module path and project dependency configuration.
IDE Import Management
IDEs can add and organize imports automatically. Developers should still understand imports because package relationships affect compilation and architecture.
Common Mistakes
- Thinking import installs a dependency
- Assuming wildcard imports include subpackages
- Creating ambiguous imports
- Using too many static imports
- Confusing source naming with runtime dependency loading
Practice
Import List and ArrayList. Replace an import with a fully qualified name. Demonstrate a same-name conflict. Use a static import. Explain why an import cannot replace Maven or Gradle dependency configuration.
Frequently Asked Questions
What is import?
A source-level declaration that lets code use an accessible type or static member by a simpler name.
Does import install a dependency?
No.
What is static import?
It allows selected static members to be used without the declaring type name.
Conclusion
Mastering this topic gives you a stronger foundation for multi-file Java programs and professional development. Focus on understanding the concept, writing small examples yourself, testing edge cases, and recognizing how the feature fits into larger application structure.
Learn More
- Java classes and objects
- Java data structures
- Java exception handling
- Java project structure
- Java testing and debugging
- Java application development
Continue Learning Java
Continue through the Java path: methods → parameters and arguments → overloading → scope and variables → packages → imports → access modifiers → classpath and project structure. These concepts prepare you for object-oriented programming and larger Java applications.
Extended Practice
- Write the main example without copying it.
- Change inputs and test normal, boundary, and invalid cases.
- Explain the execution flow in your own words.
- Refactor one example to make its responsibility clearer.
- Explain where the concept appears in a real Java application.