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

OopStatic MethodsStatic Variables

Oop Problem Overview


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

Oop Solutions


Solution 1 - Oop

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 - Oop

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 - Oop

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 - Oop

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 - Oop

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

Solution 6 - Oop

Static variables and static methods are bound to the class, and not an instance of the class.

Static methods should not contain a "state". Anything related to a state, should be bound to an instantiated object, and not the class.

Solution 7 - Oop

One common usage of static methods is in the named constructor idiom. See: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8.

Solution 8 - Oop

Static Methods in PHP:

Can be called without creating a class object.

Can only call on static methods and function.

Solution 9 - Oop

Static variable is used when you want to share some info between different objects of the class.As variable is shared each object can update it and the updated value be available for all other objects as well. As static variable can be shared,these are often called as class variable.

Solution 10 - Oop

static elements are accessible from any context (i.e. anywhere in your script), so you can access these methods without needing to pass an instance of the class from object to object.

Static elements are available in every instance of a class, so you can set values that you want to be available to all members of a type.

for further reading a link!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaveedView Question on Stackoverflow
Solution 1 - OopD.C.View Answer on Stackoverflow
Solution 2 - OopPhilMYView Answer on Stackoverflow
Solution 3 - OopjmozView Answer on Stackoverflow
Solution 4 - OopDavid HodgsonView Answer on Stackoverflow
Solution 5 - OopAndrewMurphyView Answer on Stackoverflow
Solution 6 - OopTravisView Answer on Stackoverflow
Solution 7 - OopKyle LutzView Answer on Stackoverflow
Solution 8 - OopIhsanullah khanView Answer on Stackoverflow
Solution 9 - OopsunnyView Answer on Stackoverflow
Solution 10 - OopPkrishnaView Answer on Stackoverflow