How do I get the name of a class?

package org.kodejava.example.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

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply