How to get the name of a class without the package?

后端 未结 3 558
余生分开走
余生分开走 2020-12-12 14:23

In C# we have Type.FullName and Type.Name for getting the name of a type (class in this case) with or without the namespace (package in java-world)

相关标签:
3条回答
  • 2020-12-12 14:31

    Class.getSimpleName()

    Returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.

    The simple name of an array is the simple name of the component type with "[]" appended. In particular the simple name of an array whose component type is anonymous is "[]".

    It is actually stripping the package information from the name, but this is hidden from you.

    0 讨论(0)
  • 2020-12-12 14:37

    The following function will work in JDK version 1.5 and above.

    public String getSimpleName()
    
    0 讨论(0)
  • 2020-12-12 14:53

    If using a StackTraceElement, use:

    String fullClassName = stackTraceElement.getClassName();
    String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
    
    System.out.println(simpleClassName);
    
    0 讨论(0)
提交回复
热议问题