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