Get an object’s class name

With this example we are going to demonstrate how to get an object’s class name. We
can get the class name for any object as shown in the steps below:

  • We create a new Class object.
  • We set the Class object to String.class and get its name using getName() API method of Class.
  • We follow the same steps for other objects, such as java.awt.geom.Ellipse2D, int, int[], String[] and Void.Type.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import java.awt.geom.Ellipse2D;

public class GetAnObjectClassName {
	public static void main(String[] args) {
		Class<?> clazz = null;
		clazz = String.class;
		System.out.println("String: " + clazz.getName());
		clazz = Ellipse2D.Double.class;
		System.out.println("Ellipse2D.Double: " + clazz.getName());
		clazz = int.class;
		System.out.println("int: " + clazz.getName());
		clazz = int[].class;
		System.out.println("int[]: " + clazz.getName());
		clazz = String[].class;
		System.out.println("String[]: " + clazz.getName());
		clazz = Void.TYPE;
		System.out.println("Void.TYPE: " + clazz.getName());



String: java.lang.String
Ellipse2D.Double: java.awt.geom.Ellipse2D$Double
int: int
int[]: [I
String[]: [Ljava.lang.String;
Void.TYPE: void

This was an example of how to get an object’s class name in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Back to top button