I\'m doing code review and came across a class that uses all static methods. The entrance method takes several arguments and then starts calling the other static methods passin
Well, personnally, I tend to think that a method modifying the state of an object should be an instance method of that object's class. In fact, i consider it a rule a thumb : a method modifying an object is an instance method of that object's class.
There however are a few exceptions :
In fact, I consider the static keyword as what it is : an option that should be used with care since it breaks some of OOP principles.
It depends on whether the passed arguments can really be classified as state.
Having static methods calling each other is OK in case it's all utility functionality split up in multiple methods to avoid duplication. For example:
public static File loadConfiguration(String name, Enum type) {
String fileName = (form file name based on name and type);
return loadFile(fileName); // static method in the same class
}
Does it help to rephrase the question:
Can you describe the data that the static methods operates on as an entity having:
In that case it should be an instantiated object, otherwise it may just be a bunch of related functions, much like a math library.
"state of a class is ...passed around amongst static methods using arguments?" This is how procedual programming works.
A class with all static methods, and no instance variables (except static final constants) is normally a utility class, eg Math. There is nothing wrong with making a unility class, (not in an of itself) BTW: If making a utility class, you chould prevent the class aver being used to crteate an object. in java you would do this by explictily defining the constructor, but making the constructor private. While as i said there is nothing wrong with creating a utility class, If the bulk of the work is being done by a utiulity class (wich esc. isn't a class in the usual sense - it's more of a collection of functions) then this is prob as sign the problem hasn't been solved using the object orientated paradim. this may or maynot be a good thing
The entrance method takes several arguments and then starts calling the other static methods passing along all or some of the arguments the entrance method received. from the sound of this, the whole class is just effectivly one method (this would definatly be the case is al lthe other static methods are private (and are just helper functions), and there are no instance variables (baring constants)) This may be and Ok thing, It's esc. structured/procedual progamming, rather neat having them (the function and it's helper)all bundled in one class. (in C you'ld just put them all in one file, and declare the helper's static (meaning can't be accesses from out side this file))