Sub topic 6
Importing a Single Class
Assume you can write and run a basic Java class and understand classes, methods, and fields. This chapter shows how to use one class from another package without repeating its full package name.
Why import a class?
A Java package is a namespace that groups related classes. A class's fully qualified name is its package name followed by its class name. For example:
com.example.shop.model.ProductThe short name Product is called the simple name. An import declaration tells Java that this simple name refers to one specific class.
Imports improve readability. They do not copy a class into your file or change where the class lives. They provide a name that the compiler can resolve.
flowchart TD
A["Main.java"] --> B["import com.example.shop.model.Product;"]
B --> C["Product"]
C --> D["com.example.shop.model.Product"]Adding a single-type import
The package declaration comes first. Import declarations come next. The class declaration follows them.
Suppose the project has this class in src/com/example/shop/model/Product.java:
package com.example.shop.model;
public class Product {
private final String name;
private final double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}A class in another package can import and use Product:
package com.example.shop;
import com.example.shop.model.Product;
public class Main {
public static void main(String[] args) {
Product product = new Product("Pen", 1.50);
System.out.println(product.getName());
System.out.println(product.getPrice());
}
}The statement import com.example.shop.model.Product; is a single-type import. It imports exactly one named class. After the import, Product can be used wherever that class name is valid.
An import must name the class, not the source file path. The package and class names are separated by periods, and the statement ends with a semicolon.
Using the fully qualified name instead
An import is optional. You can write the fully qualified name directly:
package com.example.shop;
public class Main {
public static void main(String[] args) {
com.example.shop.model.Product product =
new com.example.shop.model.Product("Pen", 1.50);
System.out.println(product.getName());
}
}This is useful when two packages contain classes with the same simple name. Java cannot choose between two single-type imports that both define Product.
What Java resolves automatically
Classes in the same package can use one another by simple name without an import. Java also implicitly makes the classes in java.lang available. That is why ordinary code can use String, System, and Math without writing imports.
Classes from other packages, such as java.util.ArrayList or com.example.shop.model.Product, normally need an import or a fully qualified name.
Use one single-type import for each external class that your file uses. Keep the import section after the package declaration and before the class declaration. Most IDEs can add and organize these declarations, but understanding them helps you diagnose compiler errors.
Exercise
What import declaration lets a class in package com.example.shop use the class com.example.shop.model.Product by its simple name?
Show answer
import com.example.shop.model.Product;