Sub topic 5
Fully Qualified Names in Java
Assume you can declare a package and run a basic Java program. This chapter focuses on how Java identifies a type precisely, especially when different packages contain classes with the same simple name.
What a fully qualified name contains
A fully qualified name (FQN) is the complete name of a class or interface. It combines the package name and the type's simple name with a dot:
package.name.TypeNameFor example, the fully qualified name of Product in the com.example.shop.model package is:
com.example.shop.model.ProductThe package declaration establishes this identity:
package com.example.shop.model;
public class Product {
private final String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
}The source file should normally be stored as:
src/com/example/shop/model/Product.javaJava uses the FQN when it distinguishes types, checks access, and looks for compiled class files. The directory structure mirrors the package name, but the FQN is a language-level name, not a file path.
Using an FQN directly
You can write the complete name wherever a type name is allowed. This avoids an import:
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");
System.out.println(product.getName());
}
}Compile and run the program from the project directory:
javac -d out src/com/example/shop/model/Product.java src/com/example/shop/Main.java
java -cp out com.example.shop.MainThe -d out option places compiled files under directories matching their packages. The -cp out option tells the Java launcher that out is the classpath root. The launcher then finds com.example.shop.Main at:
out/com/example/shop/Main.classThe name passed to java is an FQN. Do not use com/example/shop/Main there, and do not include .class.
FQNs and imports
An import creates a shorter name for a type in the current source file. It does not rename the class or change its FQN.
import com.example.shop.model.Product;After this declaration, Product means com.example.shop.model.Product in that file. These two declarations refer to the same type:
Product first = new Product("Pen");
com.example.shop.model.Product second =
new com.example.shop.model.Product("Notebook");A type can also be identified by its binary name when nested types are involved. For example, the source-level name java.util.Map.Entry identifies the Entry interface nested inside Map. The exact compiled representation uses $, such as java.util.Map$Entry, but Java source normally uses dots.
Why FQNs matter when names collide
Two packages can contain types with the same simple name. Java cannot use two single-type imports with the same simple name:
import java.util.Date;
// import java.sql.Date; // Causes a conflictImport one type and write the other by its FQN:
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
Date utilDate = new Date();
java.sql.Date sqlDate =
new java.sql.Date(utilDate.getTime());
System.out.println(utilDate.getTime() == sqlDate.getTime());
}
}This makes each variable's type explicit and resolves the conflict without changing either class.
Common mistakes
- Passing a path instead of an FQN to
java. - Omitting the package from a class whose source declares one.
- Assuming an import changes where the class is stored.
- Using the wrong class when two packages contain the same simple name.
- Writing a package name with uppercase letters when the project convention uses lowercase names.
Exercise
In a class that imports java.util.Date, how can you declare a variable of type java.sql.Date?
Show answer
Write the other type by its fully qualified name: java.sql.Date value;