Friday, July 1, 2016

Determine the class name of an object

Sometimes it is required at runtime to get the class name of an object.Before we need to do any introspection on an object , we need to find its java.lang.Class object.All types in java like all object types,all primitive types,all array types etc have an associated  java.lang.Class object. Please note that I am using  Upper Case C for java.lang.Class and Lower Case c for class in general. If we know the name of a class at compile time we can find the Class(java.lang.Class) object of that class by using below syntax
 Class myclassObj = MyClass.class 
But in runtime to find the Class(java.lang.Class) object of the given object below syntax is used.
 Class myclassObj = myobj.getClass() 
Now to find the String representation of the name of the  class of we use the getName() method on the Class object.

String className=myClass.getName();
getName() will give us the fully qualified class name i.e class name along with package name.

If we  want to get only class name without package name prefixed with ,we can use getSimpleName() method on the Class object.
Let's see a working example

package com.brainatjava.test;

import java.util.HashMap;
import java.util.Map;


public class ClassTest {
    public static void main(String[] args) throws InterruptedException {

        String s ="good";
        System.out.println("class name is: " + s.getClass().getSimpleName());

        Map<String, String> m = new HashMap<>();
        System.out.println("class name is: " + m.getClass().getName());        

        Boolean b = new Boolean(true);
        System.out.println("class name is: " + b.getClass().getName());

        StringBuilder sb = new StringBuilder();
        Class c = sb.getClass();
        System.out.println("class name is: " + c.getName());

        int[] a=new int[3];
        System.out.println("class name is: " + a.getClass().getName());

        Integer[] in=new Integer[3];
        System.out.println("class name is: " + in.getClass().getName());

        double[] du=new double[3];
        System.out.println("class name is: " + du.getClass().getName());

        Double[] d=new Double[3];
        System.out.println("class name is: " + d.getClass().getName());
       
    }
    }

And we get the below output

class name is: String

class name is: java.util.HashMap

class name is: java.lang.Boolean

class name is: java.lang.StringBuilder

class name is: [I

class name is: [Ljava.lang.Integer;

class name is: [D

class name is: [Ljava.lang.Double;
If you are curious about the last four lines of the output,Then please go through the below explanation.

What is [I , [D,[Ljava.lang.Integer,[Ljava.lang.Double
As we saw in the above paragraph we can get the java.lang.Class object by calling getClass() method on the object.

 If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by The Java™ Language Specification.

If this class object represents a primitive type or void, then the name returned is a String equal to the Java language keyword corresponding to the primitive type or void.

If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more '[' characters representing the depth of the array nesting. The encoding of element type names is as follows:

    Element Type             Encoding
    boolean                              Z
    byte                                    B
    char                                   C
    class or interface             Lclassname;
    double                                D
    float                                    F
    int                                       I
    long                                    J
    short                                  S
For more details please refer oracle official doc for method getName() .