Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created June 29, 2016 14:46
Show Gist options
  • Save hseritt/824ee8ea44604e63cbcb18e9021e7c16 to your computer and use it in GitHub Desktop.
Save hseritt/824ee8ea44604e63cbcb18e9021e7c16 to your computer and use it in GitHub Desktop.
Dynamic creation of class with parameters in the constructor
// See http://stackoverflow.com/questions/9886266/is-there-a-way-to-instantiate-a-class-by-name-in-java
Class<?> agent = Class.forName(agentModel.getClassName());
Constructor<?> constructor = agent.getConstructor(AgentModel.class);
Object runningAgent = constructor.newInstance(agentModel);
Thread agentThread = new Thread((Runnable) runningAgent);
agentThread.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment