01 / 20

Imports &
Qualified Names

Master how Java organizes classes into packages, and learn the two ways to reference them: import statements and fully qualified names.

Java

What Is a Package?

A package is a namespace that groups related Java classes and interfaces. Think of it as a folder system for your code: it prevents naming conflicts and keeps large projects organized.

java java.util java.io ArrayList Scanner File PrintWriter Packages act as folders: each class has a unique address e.g. java.util.Scanner, java.io.File

Why Do We Need Imports?

The Java standard library contains thousands of classes across hundreds of packages. Without imports, you would need to type the full address of every class each time you use it.

Without Import
java.util.ArrayList<String> list = new java.util.ArrayList<>();
With Import
ArrayList<String> list = new ArrayList<>();

The java.lang Exception

One package is always imported automatically: java.lang. That is why you can use String, Math, System, and Integer without writing any import statement.

Auto-imported
String, Math, System, Integer, Object, Exception
Must import
Scanner, ArrayList, HashMap, File, Arrays, Collections

The import Statement

Syntax
import packageName.ClassName;
Example
// Import a single class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); } }

Wildcard * Imports

You can import all classes in a package at once using the asterisk wildcard. This does not import sub-packages.

// Import everything in java.util import java.util.*; public class Demo { ArrayList<String> names = new ArrayList<>(); Scanner sc = new Scanner(System.in); HashMap<String, Integer> map = new HashMap<>(); }

Fully Qualified Names

Instead of importing, you can use a class's full address directly in your code. This is called a fully qualified name (FQN).

// No import needed: use the full path inline public class Main { public static void main(String[] args) { java.util.Scanner sc = new java.util.Scanner(System.in); } }

import vs Fully Qualified Name

import Statement Fully Qualified Name
Location Top of the file Inline, wherever class is used
Scope Whole file Single usage point
Readability Clean, short names Verbose, long paths
Name conflicts Can cause ambiguity Resolves ambiguity
Typical use Day-to-day coding Resolving naming collisions

Resolving Name Conflicts

Both java.util and java.sql contain a class called Date. If you import both with wildcards, the compiler cannot tell which Date you mean.

import java.util.*; import java.sql.*; // ERROR: reference to Date is ambiguous Date d = new Date();
Solution: use one import + one FQN
import java.util.Date; // import one Date utilDate = new Date(); java.sql.Date sqlDate = // qualify the other new java.sql.Date(System.currentTimeMillis());

Step-by-Step Checklist

1
Identify the class you need. For example, you want to use Scanner.
2
Find its package. Scanner lives in java.util. Check the API docs or your IDE.
3
Check for conflicts. Is there another class with the same name in a different package you are using?
4
No conflict: add import java.util.Scanner; at the top.
5
Conflict exists: import one class and use the fully qualified name for the other.

Example 1: Single Import

Write a program that reads a number from the user and prints its square.

import java.util.Scanner; public class SquareIt { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int n = in.nextInt(); System.out.println("Square: " + (n * n)); } }

Example 2: Multiple Imports

Store names in a list and write them to a file.

import java.util.ArrayList; import java.io.PrintWriter; import java.io.IOException; public class NameSaver { public static void main(String[] args) throws IOException { ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); PrintWriter pw = new PrintWriter("names.txt"); for (String name : names) { pw.println(name); } pw.close(); } }

Example 3: Name Collision

Use both java.util.Date and java.sql.Date in the same file.

import java.util.Date; // import one explicitly public class DualDate { public static void main(String[] args) { // Uses the imported java.util.Date Date now = new Date(); // Uses fully qualified name for java.sql.Date java.sql.Date sqlDate = new java.sql.Date( now.getTime() ); System.out.println(now); System.out.println(sqlDate); } }

File Layout Order

1 package declaration (optional) 2 import statements (0 or more) 3 class declaration (required) This order is mandatory in Java. Violating it causes a compile-time error.

Common Mistakes

✕ Wildcard Sub-packages
java.util.* does not import java.util.stream.*. Each sub-package needs its own import.
✕ Importing java.lang
Writing import java.lang.String; is harmless but unnecessary. The compiler already handles it.
✕ Ambiguous Wildcard Clash
Using import java.util.*; and import java.sql.*; together causes ambiguity for class Date.

Question 1 of 5

Which import statement correctly allows you to use ArrayList in your code?

Question 2 of 5

Which package is automatically imported into every Java file?

Question 3 of 5

What does import java.util.*; import?

Question 4 of 5

You need both java.util.Date and java.sql.Date in the same file. What is the best approach?

Question 5 of 5

What happens when you try to compile this code?