Of course! Let's break down the import statement in Java. This is a fundamental concept for organizing and using code.

What is the import Statement?
In simple terms, the import statement is Java's way of telling the compiler: "I plan to use code from this specific package or class."
Think of it like using a contact in your phone. Instead of typing the full phone number every time you want to call someone, you just use their name (e.g., "Mom"). The import statement lets you use a class's "name" (e.g., ArrayList) instead of its full "phone number" (e.g., java.util.ArrayList).
The Problem import Solves: Packages
Java organizes related classes into packages. A package is essentially a namespace that prevents class names from conflicting. For example, both Java's I/O library and Google's Guava library have a class named Base64. Packages distinguish them: java.io.Base64 vs. com.google.common.io.Base64.
Without import, every time you wanted to use a class, you'd have to write its fully qualified name (FQN), which is the package name followed by the class name.

Example without import:
public class WithoutImport {
public static void main(String[] args) {
// We must use the FULL name of the class
java.util.ArrayList<String> names = new java.util.ArrayList<>();
java.util.Date today = new java.util.Date();
names.add("Alice");
names.add("Bob");
System.out.println("Today's date: " + today);
System.out.println("Names: " + names);
}
}
This works, but it's very verbose and tedious. The import statement cleans this up.
How to Use the import Statement
You place import statements at the top of your Java source file, after the package declaration (if any) and before the class definition.
A. Importing a Single Class
This is the most common form. You import one specific class from a package.

Syntax: import packageName.ClassName;
Example with import:
// Import only the ArrayList class from the java.util package
import java.util.ArrayList;
// Import only the Date class from the java.util package
import java.util.Date;
public class WithImport {
public static void main(String[] args) {
// Now we can use the class name directly!
ArrayList<String> names = new ArrayList<>();
Date today = new Date();
names.add("Charlie");
names.add("David");
System.out.println("Today's date: " + today);
System.out.println("Names: " + names);
}
}
This is much cleaner and easier to read.
B. Importing an Entire Package (Wildcard Import)
You can use a wildcard () to import all the public classes from a specific package. This is convenient but can have minor drawbacks, which we'll discuss later.
Syntax: import packageName.*;
Example:
// Import ALL public classes from the java.util package
import java.util.*;
public class WithWildcardImport {
public static void main(String[] args) {
// We can now use ArrayList, Date, List, Set, Map, etc. without importing them individually
ArrayList<String> names = new ArrayList<>();
Date today = new Date();
System.out.println("Today's date: " + today);
System.out.println("Names: " + names);
}
}
Special Case: java.lang
The java.lang package is special. It contains fundamental classes that are so core to the Java language that the compiler automatically imports them for you. You don't (and can't) need to import them.
Examples of java.lang classes:
SystemStringObjectMathInteger(and other wrapper classes likeDouble,Boolean)Exception
This is why you can write System.out.println(...) directly without an import statement.
Static Imports (A Special Kind of Import)
There's also a static import, which allows you to import static members (fields or methods) of a class or interface. This lets you use them as if they were defined in your own class.
Syntax: import static packageName.ClassName.staticMember;
Example:
Without a static import, you'd have to write Math.PI and Math.sqrt().
public class WithoutStaticImport {
public static void main(String[] args) {
double radius = 10.0;
double area = Math.PI * radius * radius; // Must use Math.
double root = Math.sqrt(area); // Must use Math.
System.out.println("Area: " + area);
System.out.println("Square root of area: " + root);
}
}
With a static import:
// Import the PI field and the sqrt method from the Math class
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
public class WithStaticImport {
public static void main(String[] args) {
double radius = 10.0;
// Now we can use PI and sqrt directly!
double area = PI * radius * radius;
double root = sqrt(area);
System.out.println("Area: " + area);
System.out.println("Square root of area: " + root);
}
}
Use static imports sparingly, as overusing them can make your code less readable (it's not always obvious where a method or constant comes from).
Best Practices and Common Pitfalls
Should I use import com.*?
No. This is a very bad practice. It imports the entire com package, which includes thousands of third-party libraries and can lead to naming conflicts and significantly slow down compilation. Always be specific.
import vs. import static
import: Imports classes/interfaces (e.g.,import java.util.List;).import static: Imports static members (fields/methods) from a class (e.g.,import static java.lang.Math.PI;).
Do import statements affect performance?
No. The import statements are resolved by the compiler. They do not add any performance overhead at runtime. The compiled bytecode (YourClass.class) does not contain any import statements. It only contains the fully qualified names of the classes it uses.
What if two classes have the same name?
This is where the import statement can be ambiguous. If you import two classes with the same simple name from different packages, the compiler will complain.
Example of a conflict:
import java.util.Date; // From the standard library
import java.sql.Date; // From the SQL library
public class ConflictExample {
public static void main(String[] args) {
// This line will cause a COMPILE ERROR!
// The compiler doesn't know which 'Date' you mean.
Date today = new Date();
}
}
Solution: In this case, you must use the fully qualified name for at least one of them to resolve the ambiguity.
import java.util.Date; // We can still use this one with its short name
// We comment out or remove the import for java.sql.Date
public class ConflictExampleFixed {
public static void main(String[] args) {
// Use the fully qualified name for the one we want
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
// And the short name for the other
Date utilDate = new Date();
System.out.println("SQL Date: " + sqlDate);
System.out.println("Util Date: " + utilDate);
}
} 