Class

Target
public class Sample {
    private String data1;
    private String data2;

    public Sample() {
    }

    public Sample(String data1, String data2) {
        this.data1 = data1;
        this.data2 = data2;
    }
    public String data1() {
        return data1;
    }
}

Class

Get Class

// Class.forName("com.example.demo.Sample");
Class<Sample> clazz = Sample.class;

๋‚ด๋ถ€ ๊ฐ’ ์กฐํšŒ

Field[] fields = clazz.getFields();
Annotation[] annotations = clazz.getAnnotations();
Method[] methods = clazz.getMethods();
Constructor<?>[] constructors = clazz.getConstructors();

E.G ) ๋นˆ์ƒ์„ฑ์ž๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์…ฉ

Class<Sample> clazz = Sample.class;
Constructor<?> declaredConstructors = clazz.getDeclaredConstructors()[0];
Sample result =(Sample) declaredConstructors.newInstance();

Last updated