Using Imported Classes
Estimated time to read: 2 minutes
Overview¶
When a class has been imported its name can be used without the qualification.
In this example, Date
is resolved to java.util.Date
at compilation time.
package io.entityfour;
import java.util.Date;
public class DatePrinter(){
public void printDate(Date aDate) {
System.out.println(aDate.toString());
}
}
Class Name Table¶
The Name Table below is an example of classes used within the Car
class that has been generated throughout this module.
LocalDate | java.time.LocalDate |
Period | java.time.Period |
min | java.loang.Math.min |
... | io.entityfour.* (Implicit) |
String | java.lang.String |
... | java.lang.* (Implicit) |
Namespace Clashes¶
It is possible to have a namespace clash in Java, despite the packaging system!
Consider the example below:
package io.entityfour;
import java.util.Date;
import java.sql.Date;
public class DatePrinter {
public void printDate(Date aDate){
System.out.println(aDate.toString());
}
public void printSQLDate(Date aDate){
System.out.println(aDate.toString());
}
}
Both java.util.Date
and java.sql.Date
have been imported. This causes a name collision within the imported classes.
This can be resolved by using the fully qualified class name for one or both of the classes.
package io.entityfour;
import java.util.Date;
public class DatePrinter {
public void printDate(Date aDate){
System.out.println(aDate.toString());
}
public void printSQLDate(java.sql.Date aDate){
System.out.println(aDate.toString());
}
}
Implicit Imports¶
All Java class file implicitly include the following import statement: import java.lang.*;
java.lang is a package that supports a set of classes that provide essential Java language and runtime services. This includes:
- String - A class for manipulating immutable character strings
- StringBuilder - A class for dynamically building mutable character strings
- System - A class for interfacing with the operating system