chandu said:
hello,
i want to know usage of static methods in a class.
is it advantageous or disadvantage to use more static methods in a class.
Static methods are useful when all of the information needed in order
to perform a calculation (logic) can be passed as arguments to the
method. So, the method does not need any of the object's state (fields)
in order to do its work.
I prefer static methods when I have some business logic / calculation
that needs only a few pieces of information (parameters), because when
I see the keyword "static" I know that I don't have to read the method
to see what bits of the object's state it uses (or modifies).
Everything is in the parameter list.
(Of course, a static method can make use of static state, but then IMHO
a class with more than a few items of static state is starting to look
more like something that should be a singleton.)
That said, programming with lots of static methods and avoiding
instance methods smacks of structured programming... someone who hasn't
quite graduated into the object-oriented world. Many times what are
written as static methods really belong as instance methods on one of
the parameters' types. For example, a static method that takes as one
of its parameters a class that you wrote... you should consider the
possibility of making it an instance method of that class and see if
that makes more sense. It's nicer to call myString.Trim() rather than
String.Trim(myString), even though both implementations are possible. I
usually ask myself, "Is this static method really an operation on some
object?" Sometimes the answer is yes, sometimes it's no.
Like any other tool, static methods have their uses. Don't be afraid of
them, but then don't overuse them, either.