When should I use static methods in a class and what are the benefits?

58,073

Solution 1

Your description of a static variable is more fitting to that found in C. The concept of a static variable in Object Oriented terms is conceptually different. I'm drawing from Java experience here. Static methods and fields are useful when they conceptually don't belong to an instance of something.

Consider a Math class that contains some common values like Pi or e, and some useful functions like sin and cos. It really does not make sense to create separate instances to use this kind of functionality, thus they are better as statics:

// This makes little sense
Math m = new Math();
float answer = m.sin(45);

// This would make more sense
float answer = Math.sin(45);

In OO languages (again, from a Java perspective) functions, or better known as methods, cannot have static local variables. Only classes can have static members, which as I've said, resemble little compared to the idea of static in C.

Solution 2

Static methods don't pass a "this" pointer to an object, so they can't reference non-static variables or methods, but may consequently be more efficient at runtime (fewer parameters and no overhead to create and destroy an object).

They can be used to group cohesive methods into a single class, or to act upon objects of their class, such as in the factory pattern.

Solution 3

Syntax (php) for static methods:

<?php
class Number {
    public static function multiply($a, $b) {
        return $a * $b;
    }
}
?>

Client code:

echo Number::multiply(1, 2);

Which makes more sense than:

$number = new Number();
echo $number->multiply(1, 2);

As the multiply() method does not use any class variables and as such does not require an instance of Number.

Solution 4

Essentially, static methods let you write procedural code in an object oriented language. It lets you call methods without having to create an object first.

Solution 5

The only time you want to use a static method in a class is when a given method does not require an instance of a class to be created. This could be when trying to return a shared data source (eg a Singleton) or performing an operation that doesn't modify the internal state of the object (String.format for example).

This wikipedia entry explains static methods pretty well: http://en.wikipedia.org/wiki/Method_(computer_science)#Static_methods

Share:
58,073
Naveed
Author by

Naveed

Coding, Cricket, Camera, Casual

Updated on July 05, 2022

Comments

  • Naveed
    Naveed almost 2 years

    I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a class, I create an object of that class and call the desired method.

    Q: Static variable in a method holds it's value even when method is executed but accessible only in its containing method but what is the best definition of static method?

    Q: Is calling the static method without creating object of that class is the only benefit of static method?

    Q: What is the accessible range for static method?

    Thanks

  • Charbel
    Charbel over 8 years
    I'm not sure I agree with the statement " It really does not make sense to create separate instances to use this kind of functionality, thus they are better as statics"
  • RecursiveExceptionException
    RecursiveExceptionException about 8 years
    little nitpicking: Number sounds more like a class representing a numerical value, not a utility class providing arithmetic operations.
  • codo-sapien
    codo-sapien almost 8 years
    @Charbel Regarding the Math example, static would make sense, unless you consider implementing the New Math.
  • andreszs
    andreszs almost 6 years
    Static methods can reference non-static methods from another class, as long as you declare global $myNonStaticClassObj inside the static functions. You may read and set values from non-static members using this method, I don't understand why I always read that static methods cannot reference non-static ones, this is not true.
  • andreszs
    andreszs almost 6 years
    But you can call a non-static method or function from a static one, by simply declaring the static function's class object as global inside a static function.
  • andreszs
    andreszs almost 6 years
    That's correct, anyway a static class can declare constant values with const in order to provide a constant, unchangeable value for itself.
  • Soulriser
    Soulriser over 4 years
    @andreszs the point is that from a static method you cannot run operations on $this as you can in an instance method. You either instantiate a new object, such as new static;, and run methods on that or you declare a singleton as in your example. But then you're essentially running the methods externally, on a new object, not within the static context.
  • Spivonious
    Spivonious about 4 years
    Just to add info, VB.NET is an OOP language and does allow static local variables in functions.