[Java] 클래스 속성명으로 객체 속성값 설정하기

Posted on 2021. 7. 19. 13:34
Filed Under Programming/Java


Customer classObj = new Customer();
System.out.println("[Before] classObj.contractId2 = "+classObj.getContractId2());
Field field;
try {
	field = classObj.getClass().getDeclaredField("contractId2");
	field.setAccessible(true);
	field.set(classObj, "설정값");
} catch (Exception e) {
	e.printStackTrace();
	
}
System.out.println("[After] classObj.contractId2 = "+classObj.getContractId2());
반응형

About

by 쑤기c

반응형