Programming/Java

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

쑤기c 2021. 7. 19. 13:34


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());
반응형