Java Reflection for Classes


Create entities package and create new class named Student as below:

package entities;

public class Student {

	private String id;
	private String name;
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Student() {
	}

	public Student(String id) {
		this.id = id;
	}

	public Student(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public Student(String id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

}




package reflection;

import java.lang.reflect.Modifier;
import entities.Student;

public class Main {

	private static void displayClassInfo(Student student) {
		Class studentClass = student.getClass();
		System.out.println("Name: " + studentClass.getName());
		System.out.println("Package Name: " + studentClass.getPackage().getName());
		System.out.println("Modifier: " + Modifier.toString(studentClass.getModifiers()));
	}

	public static void main(String[] args) {

		Student student = new Student();
		displayClassInfo(student);

	}

}
Name: entities.Student
Package Name: entities
Modifier: public