13

Assume we have some simple interface:

public interface IExecutable {
    void execute();
}

and some class that implements the interface:

public class Executor implements IExecutable {
    public static void execute() {
        System.debug('Executing from a static context...');
    }
}

Believe you or not but Executor class compiled without any errors. That means that abstract interface methods can be implemented as static:

IExecutable exe = new Executor();
Executor.execute(); // Executing from a static context...
exe.execute(); // Executing from a static context...

I didn't find any information in ApexDocs about such behavior. Is it a bug or (undocumented) intentionally designed behavior?

3
  • I don't know, but I'm asking someone who might. Hang tight.
    – sfdcfox
    Apr 30 at 19:37
  • @sfdcfox Thanks Apr 30 at 19:40
  • I think it demonstrates quite clearly (and not for the first time) that the Apex language runtime is quite unlike something like Java itself. Not a behaviour I would rely on...
    – Phil W
    Apr 30 at 20:16
8

According to Chris Peterson, this is apparently a bug that's been preserved because people are depending on it.

I think this had to be preserved in the new compiler since so many people relied on it (!!!!). It's certainly something I'd like to remove from the language, but Release Updates are expensive enough it's not a priority (yet).

Based on that, this is a bug that should not be relied on in production code. If you rely on undocumented behavior, such as this bug, you're helping contribute to that internal friction that causes bugs to not be fixed, or at least, be severely delayed.

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.