I have created an Apex InterfaceFactory that the user can use to get an instance of a specified implementation of an interface. For this I use the System.Type class. The actual name of the class can be set in a custom setting. I wanted to be able to pass some parameters to the constructor of the dynamically instantiated class. In short; this is the code I'm talking about:

public static MyInterface GetMyInterfaceImplementationByClassName(String settingName){
    String implementationClassName;
    Type t;
    MyInterface implementation = null;

    //get the actual class name from the custom setting
    Interface_Implementations__c settings = Interface_Implementations__c.getInstance(settingName);

    //just make sure there actually is a className found
    if (settings != null && settings.Class__c != null) {
        implementationClassName = settings.Class__c;
        //get the type object for the class name retrieved from the custom setting
        t = Type.forName(implementationClassName);
        //this line is where my question comes in; how can I pass parameters here
        implementation = (MyInterface)t.newInstance();

    return implementation;
  • Sadly, this cannot currently be done in Apex. Jan 7, 2013 at 16:03

2 Answers 2


Sadly, this cannot currently be done in Apex. It would be great to be able to say pass parameters to the newInstance method but it is not currently possible.

Link to documentation here:


Which doesn't explicitly say it isn't possible, but given there are no documentation i think that implies it isn't (and certainly, better men than me have told me it isn't :).

You could maybe try another approach and have a common setters on your base interface and inject parameters accordingly?

EDIT: Some more dark arts reflection tricks can be found on this link (see solution provided by @zachelrath):

Managed Package Integration without Extensions or Dependencies

  • I was thinking of using the getter-and-setter-appraoch, but constructor parameters would be so much nicer... Jan 7, 2013 at 16:24
  • @Lex tell me about it Jan 7, 2013 at 16:25
  • Getters and setters are your only manner of doing so at this point, SFDC will likely expose it at a future date. In Java it is done like so: Class.getDeclaredConstructors(types list).newInstance(args list);
    – Mark Pond
    Jan 7, 2013 at 16:47

I have simulated this many times by making my dynamically instantiated classes implement an interface that declares something like init(String p1, String p2) and then store those initialization params in the same place that the class is stored.

It's barely much more work than if the reflection API included passing parameters to constructors.

  • 4
    Can you provide an example of this?
    – R Roesler
    Dec 26, 2019 at 1:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .