Close

Java Reflection - Class.getCanonicalName() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public String getCanonicalName()

Returns the canonical name of the underlying class or returns null if the underlying class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a canonical name).

Returns:
the canonical name of the underlying class if it exists, and null otherwise.


Examples


package com.logicbig.example.clazz;

import java.math.BigDecimal;

public class GetCanonicalNameExample {

public static void main(String... args) {
String canonicalName = BigDecimal.class.getCanonicalName();
System.out.println(canonicalName);

//an anonymous class
canonicalName = new Runnable() {
public void run() {
}
}.getClass().getCanonicalName();
System.out.println(canonicalName);

//java 8 lambda
Runnable runnable = () -> {
};
canonicalName = runnable.getClass().getCanonicalName();
System.out.println(canonicalName);

canonicalName = String[].class.getCanonicalName();
System.out.println(canonicalName);
}
}

Output

java.math.BigDecimal
null
com.logicbig.example.clazz.GetCanonicalNameExample$$Lambda$41/1803993120
java.lang.String[]




See Also