Home » Java programming language

How to access MetaData Of a class using Java?

In this article, we are going to learn that how can we access metadata of a class using java? MetaData of a class includes the class name, fields name, methods, constructor etc.
Submitted by Jyoti Singh, on February 27, 2018

To get the MetaData, we use Java reflection API. Java refractor class provides the methods to get the metadata of a class. Here, we are going to use the following methods.

  1. class.forName()
    This method loads a class which is provided as the parameter , if class is not found it will throw an error.
  2. isInterface()
    This function checks whether a class is an interface or not and returns a Boolean value.
  3. getDeclaredFields()
    This returns all fields name of a class.
  4. getDeclaredMethods()
    This returns all methods name of a class.
  5. getDeclaredConstructor()
    This returns all constructor name of a class.

Let’s understand them more clearly by this example. Here, we have a class name Product with three fields in it and an interface named NoteBook.

package logicProgramming;
* In This Program We are Going to Get Meta Data Of Running Class
* And Going To Examine And Change The Behavior Of Class
import java.lang.Class;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//an interface 
interface NoteBook
	int bookId=100;

//a class 
class Product{ 
	private int productId;
	private String name;
	public long price;

	public Product(int productId,String name,long price)
	public Product()

	//this function prints the data of the object
	public void putProduct()
	{System.out.println("ProductId :"+this.productId+"\nName :"+this.name+"\nPrice"+this.price);}

	public String toString()
		return("ProductId :"+this.productId+"\nName :"+this.name+"\nPrice"+this.price);
		// to return object so that object values are printed rather 
		//than it's hexadecimal address

//main class
public class ExClassMetaData_ReflectionAPI_JAVA {
	public static void main(String arg[])
			//Class.forName(ClassName)    Use For Loading The Class		
			Class cs=Class.forName("logicProgramming.Product");
			System.out.println(cs.getName()); //getName() function is getting the name of the class 

			//getClass()	 Is also used To get The meta Data Of Class
			Product P=new Product();
			Class pcls=P.getClass();//getting the meta data of Product class

			//public boolean isInterface()   tells that whether 
			//the current class is Interface or a Simple Class
			//Book Is a Interface so It Will print True...	

			//public Field[] getDeclaredFields()	
			//returns an array of name of all fields of this class.	
			Field fields[] =cs.getDeclaredFields();
			System.out.println("Fields Of product Class");
			//loop to print the fields name of the class 
			for(int i=0;i<fields.length;i++)
			//public Method[] getDeclaredMethods()	
			//returns an array of name of all methods of this class.    
			Method methods[]=pcls.getDeclaredMethods();
			System.out.println("Methods Of product Class");
			//loop to print the methods name of the class 
			for(int i=0;i<methods.length;i++)
			//public Constructor[] getDeclaredConstructors()	
			//returns the total number of constructors of this class.
			Constructor<Product> constructors[]=pcls.getDeclaredConstructors();
			System.out.println("Constructors Of product Class");
			//loop to print the constructor name of the class 
			for(int i=0;i<constructors.length;i++)
		catch(ClassNotFoundException e)


Access MetaData in Java - output

Here, we have all the meta data of the class.

Note: "logicProgramming" is the name of the package , replace it with your package name

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.