Field
์ํ ์ฝ๋
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;
}
public String data1(String raw) {
return data1 + raw;
}
}
Field
Sample sample = new Sample("1", "2");
Class<Sample> clazz = Sample.class;
// ์ ์ฒด ์กฐํ
Field[] fields = clazz.getFields();
// declared ๋ public์ด ์๋ ๋ชจ๋ ํ๋๋ฅผ ์กฐํํ๋ค.
Field data1 = clazz.getDeclaredField("data1");
ํ๋ ๊ฐ ๋ณ๊ฒฝ
Sample sample = new Sample("1", "2");
Class<Sample> clazz = Sample.class;
Field data1 = clazz.getDeclaredField("data1");
data1.setAccessible(true);
data1.set(sample, "New");
System.out.println(sample.data1());
// New
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;
}
public String data1(String raw) {
return data1 + raw;
}
}
Field
get Field
Sample sample = new Sample("1", "2");
Class<? extends Sample> clazz = sample.getClass();
Field[] fields = clazz.getFields();
Field data1 = clazz.getField("data1");
Field[] declaredFields = clazz.getDeclaredFields();
Field data11 = clazz.getDeclaredField("data1");
Last updated