The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang  [58 examples] > Classes  [11 examples]

e60. Getting the Name of a Class Object

    // Get the fully-qualified name of a class
    Class cls = java.lang.String.class;
    String name = cls.getName();        // java.lang.String
    
    // Get the fully-qualified name of a inner class
    cls = java.util.Map.Entry.class;
    name = cls.getName();               // java.util.Map$Entry
    
    // Get the unqualified name of a class
    cls = java.util.Map.Entry.class;
    name = cls.getName();
    if (name.lastIndexOf('.') > 0) {
        name = name.substring(name.lastIndexOf('.')+1);  // Map$Entry
    }
    // The $ can be converted to a .
    name = name.replace('$', '.');      // Map.Entry
    
    
    // Get the name of a primitive type
    name = int.class.getName();         // int
    
    // Get the name of an array
    name = boolean[].class.getName();   // [Z
    name = byte[].class.getName();      // [B
    name = char[].class.getName();      // [C
    name = short[].class.getName();     // [S
    name = int[].class.getName();       // [I
    name = long[].class.getName();      // [J
    name = float[].class.getName();     // [F
    name = double[].class.getName();    // [D
    name = String[].class.getName();    // [Ljava.lang.String;
    name = int[][].class.getName();     // [[I
    
    // Get the name of void
    cls = Void.TYPE;
    name = cls.getName();               // void

 Related Examples
e59. Getting a Class Object
e61. Determining If a Class Object Represents a Class or Interface
e62. Getting the Superclass of an Object
e63. Getting the Superclass of a Class Object
e64. Listing the Interfaces That a Class Implements
e65. Listing the Interfaces That an Interface Extends
e66. Getting the Package of a Class
e67. Determining from Where a Class Was Loaded
e68. Loading a Class That Is Not on the Classpath
e69. Dynamically Reloading a Modified Class

See also: Arrays    Assertions    Commands    Numbers    Objects    Strings    System Properties    Threads   


© 2002 Addison-Wesley.