2

I was wondering what are the implications of declaring a method as static. I understand that when declared as static, I can use the method without instantiating the class. But when should I declare a method as static and what are the implications when I declare a method as static.

Thanks in advance for any help/clarification on this topic.

hli1022
  • 25
  • 1
  • 5

1 Answers1

5

The reasons for using a static method in Salesforce's Apex are the same as Java or other related object-oriented languages. In short, use static when the method applies to a class and not an instance of that class. For an example, I'll refer to an answer from not-just-yeti to the question Java: when to use static methods:

In a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can't be static since it's inconceivable to call the method before any Car has been constructed.

For more examples, see: Java: when to use static methods

UPDATE: I realized this example above might be a little abstract if you're coming to programming and OO design from Salesforce, so let me re-phrase with an example from the built-in Apex TimeZone class:

This class, as you'd probably expect, represents time zones around the world. Instances of this class (i.e. TimeZone objects) represent the individual named time zones (e.g. EST, CST, PST, etc). Notice the difference here -- the class defines what a time zone is in general (e.g. a defined region of the earth with a time offset of a particular number of hours), but an instance of the class (i.e. object) defines one particular time zone. For example, the instance for PST would contain data about itself like displayName = "Pacific Standard Time" and offset = -8, which you access though its instance methods getDisplayName() and getOffset(), respectively. On the other hand, if you wanted to get information about time zones in general, you would use a static method on the TimeZone class. For example, Apex defines a static method called getTimeZone() on the TimeZone class you can use to look up a TimeZone instance by id. To put this together, consider this example to get the display name for PST:

TimeZone pst   = TimeZone.getTimeZone('PST');  // static method call
String pstName = pst.getDisplayName();         // instance method call

Notice how the first line is getting an instance of a TimeZone which has the specific info about PST and then the second line is actually getting that info.

Hopefully this cleared things up and didn't make it more confusing. To sum it up, if you are accessing data or doing something about a particular instance of data, whether that be a car or a time zone, you'll want to use an instance method. On the other hand, if you are accessing data or doing something about a whole class of data, whether it be cars or time zones, you'll want to use a static method.

Community
  • 1
  • 1
ryanbrainard
  • 5,918
  • 35
  • 41
  • Thanks you for your reply. Apologies for being thick, so in the example above with the Class Car, how come I cannot declare the method setMileage as static? – hli1022 Mar 25 '13 at 15:10
  • Because then `setMileage` would not have access to the mileage of the particular car. In order to have access to the data about the particular car, it needs to be an instance method. If it was `static`, that would imply that mileage is something globally shared by all cars, which we know is not true. – ryanbrainard Mar 26 '13 at 04:33
  • I just updated my answer with an additional example; hopefully that clears things up a bit. – ryanbrainard Mar 26 '13 at 04:34
  • Hi thank you so much for the explanation. I think I am finally starting to understand the difference between an instance method and a static. Please correct me if I am mistaken, but one would declare a method as static only when you want to make the change to the entire class. This will also mean that the values will update for any other class that are using this method. However, if one were to declare a method simply as "public void myMethod" as any change to the class would only affect that instance and not the entire class. – hli1022 Mar 31 '13 at 15:52
  • Yes, that is pretty much correct. To really have it sink in, I'd recommend creating some dummy class with some static and instance fields/methods, then try using the class and see how it behaves. For example, try calling the fields/methods directly on the class (without creating a `new` instance). Which ones compile, which ones don't? Now try with instances of the class created with the `new` keyword, and go through the same exercise. Once you have instances, try updating a field on one instance and then reading it from another. How does having the `static` keyword affect that behavior? – ryanbrainard Mar 31 '13 at 20:45
  • See this post too for a helpful explanation of the considerations of when to use static methods and when to use instance methods: https://salesforce.stackexchange.com/questions/319658/question-about-static-methods – MikeA Dec 02 '20 at 21:39