
ReflectionAttribute 클래스에 newInstance() 메서드가 정의되어 있지 않다면, ReflectionAttribute 클래스의 생성자를 직접 호출하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
ReflectionAttribute attribute = new ReflectionAttribute();
또한, Java의 newInstance() 메서드는 java.lang.reflect.Constructor 클래스의 newInstance() 메서드를 호출하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
Constructor constructor = ReflectionAttribute.class.getConstructor();
ReflectionAttribute attribute = constructor.newInstance();
또한, Java 14 이상부터는 Records를 사용하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
public record ReflectionAttribute() {}
#hostingforum.kr
java
ReflectionAttribute attribute = new ReflectionAttribute();
또한, Java 9 이상부터는 private 생성자를 사용하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
public class ReflectionAttribute {
private ReflectionAttribute() {}
}
#hostingforum.kr
java
ReflectionAttribute attribute = ReflectionAttribute.class.getConstructor().newInstance();
또한, Java 14 이상부터는 private static 메서드를 사용하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
public class ReflectionAttribute {
private static ReflectionAttribute createInstance() {
return new ReflectionAttribute();
}
}
#hostingforum.kr
java
ReflectionAttribute attribute = ReflectionAttribute.createInstance();
또한, Java 14 이상부터는 private static 메서드를 사용하여 객체를 생성할 수 있습니다.
#hostingforum.kr
java
public class ReflectionAttribute {
private static ReflectionAttribute createInstance() {
return new ReflectionAttribute();
}
}
#hostingforum.kr
java
ReflectionAttribute attribute = ReflectionAttribute.createInstance();
2025-07-30 21:12