Programing

Java에서 이름으로 클래스를 인스턴스화하는 방법이 있습니까?

lottogame 2020. 9. 6. 11:53
반응형

Java에서 이름으로 클래스를 인스턴스화하는 방법이 있습니까?


나는 질문으로보고 있었다 : 이름 을 가질 때 클래스를 인스턴스화하는 방법을 설명하는 문자열 이름 에서 클래스를 인스턴스화하십시오. Java로 수행하는 방법이 있습니까? 패키지 이름과 클래스 이름을 갖게되며 특정 이름을 가진 개체를 만들 수 있어야합니다.


두 가지 방법:

방법 1-인수가없는 생성자가있는 클래스에만 해당

클래스에 인수가없는 생성자가있는 경우 메서드를 사용하여 Class객체를 가져오고이 메서드를 사용하여 인스턴스를 만들 수 있습니다 (이 메서드는 Java의 확인 된 예외를 무효화 할 수 있기 때문에 종종 악의적 인 것으로 간주됩니다 ).Class.forName()newInstance()

예를 들면 :

Class<?> clazz = Class.forName("java.util.Date");
Object date = clazz.newInstance();

방법 2

클래스에 인수가없는 생성자가없는 경우에도 작동하는보다 안전한 방법은 클래스 개체를 쿼리하여 해당 Constructor개체 를 가져오고이 개체에 대한 newInstance()메서드를 호출하는 것입니다.

Class<?> clazz = Class.forName("com.foo.MyClass");
Constructor<?> constructor = clazz.getConstructor(String.class, Integer.class);
Object instance = constructor.newInstance("stringparam", 42);

두 방법 모두 반사라고 합니다. 일반적으로 다음과 같은 것을 포함하여 발생할 수있는 다양한 예외를 포착해야합니다.

  • JVM이 클래스를 찾을 수 없거나로드 할 수 없습니다.
  • 인스턴스화하려는 클래스에 올바른 종류의 생성자가 없습니다.
  • 생성자 자체에서 예외가 발생했습니다.
  • 호출하려는 생성자가 공개되지 않았습니다.
  • 보안 관리자가 설치되어 반사가 발생하지 않습니다.

MyClass myInstance = (MyClass) Class.forName("MyClass").newInstance();

use Class.forName ( "클래스의 문자열 이름") .newInstance ();

Class.forName("A").newInstance();

이로 인해 A라는 클래스가 초기화됩니다.


를 사용하여 인스턴스를 만들기 위해 클래스의 정규화 된 이름을 더 쉽게 얻으려면 메서드를 Class.forName(...)사용할 수 있습니다 Class.getName(). 다음과 같은 것 :

class ObjectMaker {
    // Constructor, fields, initialization, etc...
    public Object makeObject(Class<?> clazz) {
        Object o = null;

        try {
            o = Class.forName(clazz.getName()).newInstance();
        } catch (ClassNotFoundException e) {
            // There may be other exceptions to throw here, 
            // but I'm writing this from memory.
            e.printStackTrace();
        }

        return o;
    }
}

그런 다음 전달하는 클래스로 돌아가는 객체를 캐스팅 할 수 있습니다 makeObject(...).

Data d = (Data) objectMaker.makeObject(Data.class);

자바 리플렉션 사용

새 객체 생성 생성자를 호출하는 것은 새 객체를 생성하는 것과 동일하기 때문에 생성자에 대한 메서드 호출과 동일한 것은 없습니다 (가장 정확하게 말하면 새 객체를 생성하려면 메모리 할당과 객체 생성이 모두 포함됨). 따라서 이전 예제와 가장 가까운 것은 다음과 같습니다.

import java.lang.reflect.*;

   public class constructor2 {
      public constructor2()
      {
      }

      public constructor2(int a, int b)
      {
         System.out.println(
           "a = " + a + " b = " + b);
      }

      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("constructor2");
           Class partypes[] = new Class[2];
            partypes[0] = Integer.TYPE;
            partypes[1] = Integer.TYPE;
            Constructor ct 
              = cls.getConstructor(partypes);
            Object arglist[] = new Object[2];
            arglist[0] = new Integer(37);
            arglist[1] = new Integer(47);
            Object retobj = ct.newInstance(arglist);
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }

which finds a constructor that handles the specified parameter types and invokes it, to create a new instance of the object. The value of this approach is that it's purely dynamic, with constructor lookup and invocation at execution time, rather than at compilation time.


Class.forName("ClassName") will solve your purpose.

Class class1 = Class.forName(ClassName);
Object object1 = class1.newInstance();

String str = (String)Class.forName("java.lang.String").newInstance();

something like this should work...

String name = "Test2";//Name of the class
        Class myClass = Class.forName(name);
        Object o = myClass.newInstance();

Using newInstance() directly is deprecated as of Java 8. You need to use Class.getDeclaredConstructor(...).newInstance(...) with the corresponding exceptions.

참고URL : https://stackoverflow.com/questions/9886266/is-there-a-way-to-instantiate-a-class-by-name-in-java

반응형