

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- Using predefined class name as Class or Variable name in Java
- Get the fully-qualified name of an inner class in Java
- Can we define a method name same as class name in Java?
- Display the package name of a class in Java
- How to get css class name using Selenium?
- Get localized day name in Java
- Get file extension name in Java
- Get the name of a primitive type in Java
- Get the Name of a Member Object in Java
- Why the constructor name is same as the class name in Java?
- Is it possible to create a class without a name in Java?
Advertisements