Home » Java programming language

Java Class class getCanonicalName() method with example

Class class getCanonicalName() method: Here, we are going to learn about the getCanonicalName() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 21, 2019

Class class getCanonicalName() method

  • getCanonicalName() method is available in java.lang package.
  • getCanonicalName() method is used to return the authorized name of the underlying class.
  • getCanonicalName() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getCanonicalName() method does not throw an exception at the time of returning canonical name.

Syntax:

    public String getCanonicalName();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is String, it returns the following values based on the given case,

  • It returns the authorized name of the underlying class.
  • It returns null when the canonical name does not exist.

Example:

// Java program to demonstrate the example 
// of String getCanonicalName() method of Class 

public class GetCanonicalNameOfClass {
    public static void main(String[] args) {
        // Creating an object of the class
        GetCanonicalNameOfClass cano_class = new GetCanonicalNameOfClass();

        // Get Class by using getClass() method
        Class cl = cano_class.getClass();

        // Display Class
        System.out.println("Class :" + cl);

        // Get Canonical Name of the class by using 
        // getCanonicalName() method and stored in a 
        // String variable

        String canonical_name = cl.getCanonicalName();

        // Display Canonical Name
        System.out.print(" Class Canonical Name :" + " ");
        System.out.println(cl.getCanonicalName());
    }
}

Output

Class :class GetCanonicalNameOfClass
 Class Canonical Name : GetCanonicalNameOfClass


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.