views:

398

answers:

8

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

A: 

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.

Kyle Lutz
+7  A: 

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();
int answer = m.sin(45);

// This would make more sense
int 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.

darren
+1  A: 

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

AndrewMurphy
+1  A: 

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.

Travis
+2  A: 

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.

David Hodgson
+1  A: 

Static Methods in PHP:

Can be called without creating a class object.

Can only call on static methods and function.

Ihsanullah Khan
+2  A: 

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.

PhilMY
+1  A: 

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.

jmoz