7

When trying to execute a static method from an instance of a class

testMe tmp = New testMe();
tmp.myStaticMethod('a','b');

we get the familiar:

Static method cannot be referenced from a non static context: void myStaticMethod(Id, String)

However if the class implements an interface, instantiating a new instance of that that interface allows us to execute the static method passing in the parameters

Is this expected / documented?

global interface testIt{
    void myStaticMethod(String a, String b);
}

global class testMe implements testIt{

    global static void myStaticMethod(String a, String b){
        system.debug(a + ' - ' + b);
    }

}

The following executes just fine

System.Type apiMethod = Type.forName('ns', 'testMe');
testit tmp = (testit)apiMethod.newInstance();
tmp.myStaticMethod('a','b');

17:03:46.25 (12026653450)|USER_DEBUG|[171]|DEBUG|a - b

I was expecting that the static instance would not be able to be called but it allows me to. If this is expected great, I will go ahead and use it but if it is not expected I would prefer not to package something I cannot reverse....

I suspect it has to do with the interface since the interface method is not static rather the implementation of it is static in the class. To test the theory I tried this and it executed just fine without complaining about the static method

testIt tmp = (testIt)(New testMe());
tmp.myStaticMethod('a','b');

All answers appreciated but especially thankful if there is some documentation that can be pointed to

2

You shouldn't depend on the behavior. The documentation states:

A class static variable can’t be accessed through an instance of that class. If class MyClass has a static variable myStaticVariable, and myClassInstance is an instance of MyClass, myClassInstance.myStaticVariable is not a legal expression.

You should always declare methods as instance methods, just to be safe.

  • Its not a variable it is a method. The static method (required by the interface) is part of a class that implements the interface. The method in the interface is not static (can't be). Maybe the interface method redefines the class implementation (static modifier) of that method?? – Eric Aug 8 '17 at 1:21
  • @Eric The documentation for static nor interface is oddly silent, but the fact that you can't access the methods through direct compilation should be a red flag. I'll ask some of my contacts I have access to if it should be allowed or not, but I would seriously recommend not doing it, because if it's not in the documentation, it's not supported. – sfdcfox Aug 8 '17 at 1:42
  • Yea, thats why I asked. Eager to hear what you find out. And thanks btw – Eric Aug 8 '17 at 1:46

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.