Easy learning with example program codes

Java reflection get class object

The java.lang.Class provides the methods to examine the runtime properties of the object including its members and type information. For every type of object, the JVM instantiates an immutable instance of java.lang.Class. Using reflection, we can get the metadata of a class at run time.

We can get class object in java by using following approaches:

  • forName() method of Class class
  • getClass() method of Object class
  • the .class syntax


Note: When using forName() method we must pass the fully qualified class name.


package com.codesjava;
public interface Shape {
	public void drawShape(String color);

package com.codesjava;
public class Rectangle implements Shape{
	private int defaultLength = 10;
	private int defaultWidth = 5;
	public void drawShape(String color) {
		System.out.println("Rectangle create with following properties: ");
		System.out.println("Length: " + defaultLength);
		System.out.println("Width: " + defaultWidth);

package com.codesjava;
public class ReflectionTest {
	public static void main(String args[]){
		try {
			//Using forName() method
			Class c=Class.forName("com.codesjava.Rectangle");  
			//Using getClass() method of Object class
			Rectangle object = new Rectangle();
			c= object.getClass();
			//.class syntax
			c = Rectangle.class;
		} catch (Exception e) {



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki