Static vs Instance MethodsA static method has the static key word in the signature. By being declared static, it means it is associated with the class and not any specific instance of the class. In Apex, a static method can only be called from the class, while an instance method can only be called from an object. An important note for developers that are accustomed to other OO languages:
Often one questions whether a field or method should be declared as static or whether it should be an instance field or method. As usual, there is not a single correct answer. The performance of calling the method is virtually the same; however, since one must instantiate an object of the class to call an instance method, there is a small hit to overall performance, cpu time, and heap size. Additional considerations:
In most cases, I prefer to use static methods for utility methods, and reserve them only for less complex functionality I do not care to mock for testing. For static fields, the answer is simpler: if there only needs to be one value for all instances, use a static field, otherwise use a method field.
|