Canonical Name Java
What is the canonical name?
The canonical name is nothing but the standard form of a name. In Java, the canonical name is the class name along with the package name. It is usually used in the import statement. For example, java.lang.Character is the canonical name for the Character class.
When the javac compiler has a complete view of a classpath, it enforces the uniqueness of canonical names within it by clashing fully qualified class and package names at compile time.
Note that the canonical name of the class is not the same as a fully qualified name. There is a slight difference between them.
For every primitive type, named package, top-level class, and top-level interface, the canonical name is the same as the fully qualified name. Beside this, each member class, member interface, and array type may have a canonical name:
How to get a canonical name for a Java class?
The Java Class class provides three different methods to get the class name.
The method returns the canonical name for a specified class. It returns null if the class does not have any canonical name.
Let's see a Java program that finds the canonical name of a class.
The canonical name of the Class is: CanonicalName
Similarities Between Canonical Name and Fully Qualified Name
Difference Between Canonical Name and Fully Qualified Name
The following code snippet clearly differentiates between the canonical name and fully qualified name.
Both p1.A.B and p1.C.B are fully qualified names that represent the member class B. But only p1.A.B is its canonical name.
Next TopicMethod Chaining in Java