- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Canonical Name for a class in Java
The canonical name of a class can be obtained using the java.lang.Class.getCanonicalName() method. This method returns the canonical name of the underlying class and returns null if there is no canonical name for the underlying class.
A program that demonstrates the getCanonicalName() method to obtain the canonical name is given as follows −
Example
package Test; import java.lang.*; public class Demo { public static void main(String[] args) { Demo obj = new Demo(); Class c = obj.getClass(); System.out.println("The canonical name of the underlying Class is: " + c.getCanonicalName()); } }
Output
The canonical name of the underlying Class is: Test.Demo
Now let us understand the above program.
In the main() method, an object obj of class Demo is created. Then the getClass() method is used to get the runtime class of object obj which is stored in c. Finally the getCanonicalName() method is used to return the canonical name of the underlying class which is class Demo. A code snippet which demonstrates this is as follows −
Demo obj = new Demo(); Class c = obj.getClass(); System.out.println("The canonical name of the underlying Class is: " + c.getCanonicalName());
- Related Articles
- What is the difference between simple name, canonical name and class name in a Java class?
- Get the class name for various objects in Java
- Get the unqualified name of a class in Java
- Get the fully-qualified name of a class in Java
- Get the fully-qualified name of an inner class in Java
- Canonical Tags: A Simple Guide for Beginners
- Using predefined class name as Class or Variable name in Java
- Display the package name of a class in Java
- Can we define a method name same as class name in Java?
- Get all declared fields from a class in Java
- Get a value from Unit Tuple class in Java
- Get a value from Pair Tuple class in Java
- When can a .class file get created in Java?
- How many ways can get the instance of a Class class in Java?
- Get class from an object in Java

Advertisements