package java.lang
public final class Class<T> implements Serializable, GenericDeclaration, Type, AnnotatedElement {
private final ClassLoader classLoader;
private Class(ClassLoader loader) {
classLoader = loader;
}
}
Instances of the class Class represent classes and interfaces in a running Java application.
An enum class and a record class are kinds of class; an annotation interface 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 a Class object is constructed automatically by the Java Virtual Machine when a class is derived from the bytes of a class file through the invocation of one of the following methods:
Class 객체
로 표현됨Class 객체
를 통해 메타데이터를 제공 Class 객체
로 표현됨 Class 객체
로 표현됨 (int, double, boolean 등)class Person {
private String name;
public int age;
public Person() {}
public void sayHello() {
System.out.println("Hello!");
}
}
public class ReflectionExample {
public static void main(String[] args) {
try {
// ① Class 객체 얻기 (3가지 방법)
Class<?> clazz1 = Person.class; // 정적 방식
Class<?> clazz2 = new Person().getClass(); // 인스턴스를 통해 얻기
Class<?> clazz3 = Class.forName("Person"); // 문자열을 통해 얻기
// ② 클래스의 필드 목록 가져오기
Field[] fields = clazz1.getDeclaredFields();
System.out.println("Fields:");
for (Field field : fields) {
System.out.println("- " + field.getName());
}
// ③ 클래스의 메서드 목록 가져오기
Method[] methods = clazz1.getDeclaredMethods();
System.out.println("\nMethods:");
for (Method method : methods) {
System.out.println("- " + method.getName());
}
// ④ 클래스의 생성자 목록 가져오기
Constructor<?>[] constructors = clazz1.getConstructors();
System.out.println("\nConstructors:");
for (Constructor<?> constructor : constructors) {
System.out.println("- " + constructor.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Class.html