How do I get the name of a class?

package org.kodejava.lang;

import java.util.Calendar;
import java.math.BigDecimal;

public class ClassName {
    public static void main(String[] args) {
        // Get the name of the classes below.
        Class<?> clazz = String.class;
        System.out.println("Class Name: " + clazz.getName());

        clazz = Calendar.class;
        System.out.println("Class Name: " + clazz.getName());

        clazz = BigDecimal.class;
        System.out.println("Class Name: " + clazz.getName());
    }
}

The result of the code snippet above are:

Class Name: java.lang.String
Class Name: java.util.Calendar
Class Name: java.math.BigDecimal

How do I know if a class object is an interface or a class?

package org.kodejava.lang;

public class CheckForInterface {
    public static void main(String[] args) {
        // Checking whether Cloneable is an interface or class
        Class<?> clazz = Cloneable.class;
        boolean isInterface = clazz.isInterface();
        System.out.println(clazz.getName() + " is an interface = " + isInterface);

        // Checking whether String is an interface or class
        clazz = String.class;
        isInterface = clazz.isInterface();
        System.out.println(clazz.getName() + " is an interface = " + isInterface);
    }
}
java.lang.Cloneable is an interface = true
java.lang.String is an interface = false

How do I get interfaces implemented by a class?

The getClass().getInterfaces() method return an array of Class that represents the interfaces implemented by an object.

package org.kodejava.lang;

import java.util.Date;
import java.util.Calendar;

public class ClassInterfaces {
    public static void main(String[] args) {
        // Get an instance of Date class
        Date date = Calendar.getInstance().getTime();

        // Get all interfaces implemented by the java.util.Date class and
        // print their names.
        Class<?>[] interfaces = date.getClass().getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", date.getClass().getName(), i.getName());
        }

        // For the primitive type the interface will be an empty array.
        interfaces = char.class.getInterfaces();
        for (Class<?> i : interfaces) {
            System.out.printf("Interface of %s = %s%n", char.class.getName(), i.getName());
        }
    }
}

The java.util.Date class implements the following interfaces:

Interface of java.util.Date = java.io.Serializable
Interface of java.util.Date = java.lang.Cloneable
Interface of java.util.Date = java.lang.Comparable

How do I get package name of a class?

To get the package name of a class we can use the getClass().getPackage() method of the inspected object.

package org.kodejava.lang;

import java.util.Date;

public class GetPackageName {
    public static void main(String[] args) {
        // Create an instance of Date class, and then obtain the class package
        // name.
        Date date = new Date();
        Package pack = date.getClass().getPackage();
        String packageName = pack.getName();
        System.out.println("Package Name = " + packageName);

        // Create an instance of our class and again get its package name
        GetPackageName object = new GetPackageName();
        packageName = object.getClass().getPackage().getName();
        System.out.println("Package Name = " + packageName);
    }
}

Our code printed the following output:

Package Name = java.util
Package Name = org.kodejava.lang

How do I get super class of an object?

To get the super class of an object we can call the object’s getClass() method. After getting the class type of the object we call the getSuperclass() method to get the superclass. Let’s see the code snippet below.

package org.kodejava.lang;

public class ObtainingSuperClass {
    public static void main(String[] args) {
        // Create an instance of String class
        Object object1 = new String("Hello");

        // Get String class super class
        Class<?> clazz1 = object1.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz1);

        // Create an instance of StringIndexOutOfBoundsException class
        Object object2 = new StringIndexOutOfBoundsException("Error message");

        // Get StringIndexOutOfBoundsException class super class
        Class<?> clazz2 = object2.getClass().getSuperclass();
        System.out.println("Super Class = " + clazz2);
    }
}

The program above prints the following string:

Super Class = class java.lang.Object
Super Class = class java.lang.IndexOutOfBoundsException