Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 433
Naveed Avatar asked Jan 17 '10 06:01

Naveed


People also ask

When should static methods be used in class?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.

What is a static class and what are the benefits of it?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

When you should use static?

Basically, static is used for a constant variable or a method that is same for every instance of a class. The main method of a class is generally labeled static. In order to create a static member (block, variable, method, nested class), you need to precede its declaration with the keyword static.


2 Answers

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.

like image 25
PhilMY Avatar answered Oct 15 '22 02:10

PhilMY


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.

like image 139
D.C. Avatar answered Oct 15 '22 01:10

D.C.