java.lang.Class Class

Class represents classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

In this tutorial you can learn how to use Class class.

java.lang.Class Class Example
/* This class is supporting class for Examples of Class class */
/* Save with file name Student.java */

//Student IS THE CLASS NAME
public class Student
{
	//FIELDS OF THE Student CLASS
	public int sid;
	public String sname;
	public String scollege;

	//CONSTRUCTORS OF THE Student CLASS
	public Student(){}

	public Student(int sid, String sname, String scollege)
	{
		this.sid = sid;
		this.sname = sname;
		this.scollege = scollege;
	}

	//SETTER METHODS OF THE Student CLASS
	public void setStudentID(int sid)
	{
		this.sid = sid;
	}

	public void setStudentName(String sname)
	{
		this.sname = sname;
	}

	public void setStudentCollege(String scollege)
	{
		this.scollege = scollege;
	}

	//GETTER METHODS OF THE Student CLASS
	public int getStudentID()
	{
		return this.sid;
	}

	public String getStudentName()
	{
		return this.sname;
	}

	public String getStudentCollege(String scollege)
	{
		return this.scollege;
	}

	public String toString()
	{
		return (this.sid+" "+this.sname+" "+this.scollege);
	}
}
                        
java.lang.Class Class Example
/* Save with file name ClassExample1.java */

import java.lang.reflect.*;

public class ClassExample1
{
	public static void main(String args[])
	{
		//Student CLASS DECLARATION
		Student s;

		//CREATE INSTANCE FOR Student CLASS
		s = new Student(1, "Huda", "http://www.hudatutorials.com");

		//Class CLASS DECLARATION
		Class c;

		//CREATE INSTANCE FOR Class CLASS
		//getClass() METHOD IS Object CLASS METHOD
		//IT IS CALLED FROM Student CLASS
		//BECAUSE Object CLASS IS SUPER (PARENT) CLASS
		c = s.getClass();

		//RETURNS THE CLASS NAME
		System.out.println("Class Name : " + c.getName());

		//RETURNS THE CLASS SIMPLE NAME
		System.out.println("Class Simple Name : " + c.getSimpleName());

		//RETURNS THE CLASS CANONICAL NAME
		System.out.println("Class Canonical Name : " + c.getCanonicalName());

		//RETURNS THE CLASS PACKAGE
		//RETURNS null IF NO PACKAGE DECLARED
		System.out.println("Package of the Class : " + c.getPackage());
	}
}
                        
java.lang.Class Class Example 2
/* Save with file name ClassExample2.java */

import java.lang.reflect.*;

public class ClassExample2
{
	public static void main(String args[])
	{
		//Student CLASS DECLARATION
		Student s;

		//CREATE INSTANCE FOR Student CLASS
		s = new Student(1, "Huda", "http://www.hudatutorials.com");

		//Class CLASS DECLARATION
		Class c;

		//CREATE INSTANCE FOR Class CLASS
		//getClass() METHOD IS Object CLASS METHOD
		//IT IS CALLED FROM Student CLASS
		//BECAUSE Object CLASS IS SUPER (PARENT) CLASS
		c = s.getClass();

		//GETTING THE FIELDS OF CLASS
		Field fields[] = c.getFields();
		System.out.println("FIELDS");
		System.out.println("======");
		for(int i=0;i<fields.length;i++)
		{
			System.out.println("Field "+ (i+1) + " => " + fields[i]);
		}

		System.out.println();
		//GETTING THE CONSTRUCTORS OF CLASS
		Constructor con[] = c.getConstructors();
		System.out.println("CONSTRUCTORS");
		System.out.println("============");
		for(int i=0;i<con.length;i++)
		{
			System.out.println("Constructor "+ (i+1) + " => " + con[i]);
		}

		System.out.println();
		//GETTING THE METHODS OF CLASS
		Method m[] = c.getMethods();
		System.out.println("METHODS");
		System.out.println("=======");
		for(int i=0;i<m.length;i++)
		{
			System.out.println();
			System.out.format("Method %3d  => " + m[i], (i+1));
		}
	}
}