When to use Static Method?
I am bit confused like when can i use the static method in the programming and may be proper real world example will be helpful
Mohamed El-Damarawy9,267 Points
Well in general static means it's out of the context of an object, so you usually use it for definitive functionality that doesn't depend on a state.
Like in Factory design pattern, instead of creating an object, you use a method from a static class that returns a new object for you.
Trent Christofferson16,423 Points
You should use static when:
- The code in the method is not dependent on instance creation and is not using any instance variable.
- A particular piece of code is to be shared by all the instance methods.
- The definition of the method should not be changed or overridden.
- You are writing utility classes which should not be changed.