Search code examples
javaclassloader

Create another instance of a dynamically loaded class - Java


So I have created an instance of a class by searching through jar files with no problems, I have it set to create an instance using c.newInstance() (Is instance the right word to use here?)

Later on in the program I may want to create another instance of that class if a certain event occurs. How can I go about creating this without having to search through all of the Jar files until I find the right one and then creating it again? Is there a way to create it somehow if I still have a reference to the first one?

Please assume that I do not know the name of the classes that will be loaded until runtime and there will be multiple classes that will be loaded.

Thanks


Solution

  • Assuming "x" is the object you created ...

    x.getClass().newInstance();