What is an Apex Static Method

As you may know, APEX in salesforce is extremely similar to Java. So in researching this topic you may want to take a look at both SFDC and Java Resources.

Disclaimer here. I am still fairly new to APEX let me know if something is wrong or if there’s something I could explain better

Methods

So what are methods? A google search will tell you that the definition of a method is a collection of statements that are grouped together to perform an operation.

It would be like giving a person a recipe. That had a list of instructions. Suppose you are baking cookies. The method for baking cookies includes preheating the oven, mixing the ingredients (possibly in a particular order), and shaping the cookies. Each step is a different statement.

Static vs. Instance Methods

Static and instance methods (or variables) have a few key differences. The main difference here is that one is related to the specific class (static) and the other is related to the specific object (instance).

In Salesforce it’s generally easier to call a static method rather than an instance method. The reason being is that they call the class directly rather than calling an instance of the class.

Syntax Differences

The main syntax difference between the static and instance methods is that static is placed before the method and this will call the class individually.

Another difference between static and instance methods, is that you cannot call any instance based variables in the static method. This is because you need an instance to call the variable and not just a class.

ELI5

One of my favorite parts of the tech community is the concept of explaining things like you’re 5. This helps me a LOT when it comes to understanding complex topics.

Let’s go back to the baking metaphor. The static method applies to all instances of the class where as the instance method only applies to one instance.

When cooking a static method would be some type of skill that can be applied to multiple recipes. For example, it would be like making a roux for a pasta dish or separating egg yolks from egg whites.

Conclusion

Big congrats to you for making it through another blog post! I hope you found this helpful.

You can check out the youtube channel here

More Reading

Post navigation

1 Comment