Abdul Rahman wrote:Hi,
Appreciate if someone can explain me the scenarios when static modifier should be used with variables and methods.
Why and when should we make a method static and is there any loophole making a variable or a method static.
You can consider the classes as providing a few special services that objects can't provide
Consider the Utility methods that return some value calculated only from data passed to the method as parameters.
You should make such methods static.
Consider Math.max(double a, double b) which is a static method. It does not operate on a single instance; indeed, it would not be good if you say new Math().max(a,b) or even a.max(b).All the data that max uses comes from its two arguments, and not from any owning object
Another important point to consider when you make method static is that you must make sure there
is no chance you’ll want it to behave polymorphically.