Class Name Forms For Diagnostics

Compare getName, getSimpleName and getCanonicalName across a plain class, a nested interface, an array and an anonymous class whose canonical name is null.

JavaCli Tooling

Code

snippet
import java.util.ArrayList;
import java.util.Map;

public class ClassNameForms {

    public static void main(String[] args) {
        Class<?> plain = ArrayList.class;
        Class<?> nested = Map.Entry.class;
        Class<?> array = String[].class;
        Class<?> anon = new Runnable() {
            @Override
            public void run() {
            }
        }.getClass();

        // getName 是 JVM 名(内部 $ 记法),simple 是短名,canonical 是源码形态
        System.out.println("ArrayList:  name=" + plain.getName()
                + " simple=" + plain.getSimpleName() + " canonical=" + plain.getCanonicalName());
        System.out.println("Map.Entry:  name=" + nested.getName()
                + " simple=" + nested.getSimpleName() + " canonical=" + nested.getCanonicalName());
        System.out.println("String[]:   name=" + array.getName()
                + " simple=" + array.getSimpleName() + " canonical=" + array.getCanonicalName());
        System.out.println("anonymous:  name=" + anon.getName()
                + " simple=" + anon.getSimpleName() + " canonical=" + anon.getCanonicalName());
    }
}

How it works

Class Name Forms For Diagnostics comes down to the class ClassNameForms and the functions main and run.

Caveats

  • No dependencies section needed; the standard library is the whole story.

Using it

The primary signature is public static void main(String[] args)

Look for the ArrayList: name=, Map.Entry: name=, String[]: name= prefixes when the file runs; they tie each line to its step.

main sequences run; executing the file runs that flow once.

Key statements worth quoting

Class<?> nested = Map.Entry.class;
Class<?> plain = ArrayList.class;
Class<?> array = String[].class;

Interactive Tools for This Topic

FAQ

What does Class Name Forms For Diagnostics print when run?
Execute the whole file; each printed line is prefixed with a label like `ArrayList: name=`, `Map.Entry: name=`, `String[]: name=`.