Java
Programming
Object-Oriented Programming
Java Class
Java Terminology

What is the difference between canonical name, simple name and class name in Java Class?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, classes are fundamental building blocks. Each class can be identified in different ways depending on its use context, and they are generally referred to by three distinct names: Canonical Name, Simple Name, and Class Name. Understanding these identifiers is essential for tasks such as reflections, serialization, and when working with generic types.

Definitions and Use-Cases

1. Canonical Name

The canonical name of a class is the name that is used to write import statements and to refer to the class in the code where the full package name is needed. This name adheres to the Java Language Specification. It includes the package name followed by the class name, using fully qualified name patterns. The canonical name is absent in some cases, especially for local, anonymous inner classes or classes within method scopes.

Example:

java
1package com.example.shapes;
2
3public class Circle {
4    // Nested class example
5    public class Point {
6        // further code
7    }
8}

The canonical name of Circle would be com.example.shapes.Circle, and for Point, it would be com.example.shapes.Circle.Point.

2. Simple Name

The simple name of a class is the name without any package prefix. It does not consider whether the class is inner or nested. Simple names are used primarily within the source code where the context makes the class reference clear and unambiguous thanks to imports or package declarations.

Example:

In the context of the previous example, the simple name of Circle is just Circle and for Point, it is simply Point.

3. Class Name

The getName() method of a Class object returns the fully qualified name of the entity (class), which includes the package name followed by the class name. However, for inner classes, the class names use $ as a delimiter between the outer and inner class names rather than the dot used in canonical names. This name is mainly used in reflection for finding classes dynamically with their names, especially in scenarios involving bytecodes or compiler operations.

Example:

In the class definitions mentioned, Circle's class name is com.example.shapes.Circle. For Point, the class name is com.example.shapes.Circle$Point, showing the use of $ to separate the inner class name.

Key Differences in Table

IdentifierExample UsageContextNotable Characteristic
Canonical Namecom.example.shapes.Circle.PointFormal DocumentationFull path, including package, with dots
Simple NamePointWithin codeOnly the class name, no package or outer class
Class Namecom.example.shapes.Circle$PointReflection, BytecodeUses $ for inner classes

Additional Details and Considerations

  • Anonymous Classes: These do not have canonical names or simple names as they are not explicitly declared with a named identifier.
  • Local Classes: Similar to anonymous classes, these are classes defined within a method. They have a simple name but no canonical name.
  • Reflection: Using reflection to manipulate or access properties of a class object typically requires understanding these three naming conventions to effectively locate and utilize target classes.

Practical Implication in Java Programming

The distinction between these three types of names becomes significant when working with advanced Java features such as reflection, where manipulating classes dynamically based on their names is crucial. Additionally, developers dealing with generics, inheritance, and nested classes often encounter scenarios where differentiating between these class names helps in providing clear, maintainable code.

In summary, canonical names provide the most detailed level of identification, including packages and nested classes, simple names offer convenience within a contained context, and class names cater to system-level identification, useful in reflection and lower-level operations. Understanding these differences enhances a developer's capability to leverage the Java language fully and maneuver within its type system more effectively.


Course illustration
Course illustration

All Rights Reserved.