Java.lang.Class.getCanonicalName() Method

Advertisements


Description

The java.lang.Class.getCanonicalName() method returns the canonical name of the underlying class as defined by the Java Language Specification. It returns null if the class does not have a canonical name.

Declaration

Following is the declaration for java.lang.Class.getCanonicalName() method

public String getCanonicalName()

Parameters

  • NA

Return Value

This method returns the canonical name of the underlying class if it exists, and null otherwise.

Exception

  • NA

Example

The following example shows the usage of java.lang.Class.getCanonicalName() method.

package com.tutorialspoint;

import java.lang.*;

public class ClassDemo {

   public static void main(String[] args) {

     ClassDemo c = new ClassDemo();
     Class cls = c.getClass();

     // returns the canonical name of the underlying class if it exists
     System.out.println("Class = " + cls.getCanonicalName()); 
   }
}

Let us compile and run the above program, this will produce the following result:

Class = ClassDemo



Advertisements
Advertisements