I'm looking to dynamically instantiate a class by its name. How can I do that in Apex? I thought about a factory method with a series of if-else statements, but am not satisfied with that as it isn't a general solution.


It seems something like that is possible with:

System.Type objType = Type.forName(objName);
Object obj = objType.newInstance();
  • 4
    Please note that it has to have an empty constructor in order to do so. – Programmable Medley May 2 '13 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.