Can I Instantiate a class using the class object? What about Constructors?

后端 未结 6 766
时光说笑
时光说笑 2020-12-09 18:15

I am storing a list of classes through (Classname.class) and would like to instantiate one? Is this possible?

newInstance seems to the met

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 18:55

    You cannot construct new classes this way.

    If you have the name of a class you can use Class.forName(className) to load/reference a class.

    If you have the byte code for a class you want to create you can have a class loader load the byte code and give you the class. This is likely to be more advanced than you intended.

提交回复
热议问题