allison said:
Can someone point me to a guide on when to use static methods versus
instance methods in a class? For instance, is it bad design to have a
static method that creates an instance of another class? I am looking for a
good explanation.
Thanks in advance
Generally speaking, OOP purists tend to frown upon static methods unless
it's necessary, such as when there could not possibly be any way to have
instance state information or instance-specific methods to be called. On the
other hand, there are some cases when instantiating a class is a nuisance,
and making the methods non-static would make the task unnecessarily
non-trivial. The static methods of System.IO.File and System.IO.Directory
are good examples of this ... These could have been designed such that one
would have to create a Directory object (passing the path into a constructor
as a string) before you can get its files or access its other properties /
methods, but it made more sense from a design perspective -- that is, there
proved to be no otherwise advantage for developers, the users of the .NET
Framework -- to keep the methods static.
So, the long and short of it is, it really just depends. Ask yourself if
there could possibily be any need to instantiate an object. If not, don't
bother. Make it static. However, typically such instances should be far less
often than non-static, for the sake of OOP.
Jon