Javatpoint Logo
Javatpoint Logo

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:

  • A member class or member interface(M) declared in another class or interface (C) has a canonical name if and only if C has a canonical name. In such a case, the canonical name of the member class or member interface consists of the canonical name of another class or interface, followed by the dot(.), and the simple name of the member class or member interface.
  • An array type may also have a canonical name if and only if its component type has a canonical name. In such a case, the canonical name of the array type consists of the canonical name of the component type of the array type followed by "[]".

How to get a canonical name for a Java class?

The Java Class class provides three different methods to get the class name.

  • getSimpleName():It returns the class name without the package name.
  • getName(): It returns the name of the class with the full package name in front.
  • getCanonicalName(): It returns the canonical name of the class.

Class.getCanonicalName() Method

The method returns the canonical name for a specified class. It returns null if the class does not have any canonical name.

Syntax:

Let's see a Java program that finds the canonical name of a class.

CanonicalName.java

Output:

The canonical name of the Class is: CanonicalName

Similarities Between Canonical Name and Fully Qualified Name

  • A local class does not have a fully qualified name and canonical name.
  • Every primitive type, named package, top-level class, and top-level interface has a fully qualified name and canonical name.
  • Each member class, member interface, and array type may have a fully qualified name. For example, the fully qualified name of the type array of double is double[].

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA