Should all methods be static if their class has no member variables

java, static

Solution

The class you describe is simply just a set of functions that operate on inputs only. It's perfectly reasonable to make such functions static methods of a class. Doing so groups them logically and eliminates possible name conflicts. The class in fact acts as a namespace and nothing more.

Problem

I've just had an argument with someone I work with and it's really bugging me. If you have a class which just has methods like `calculateRisk` or/and `calculatePrice`, the class is immutable and has no member variables, should the methods be static so as not to have to create an instance of the class each time. I use the following example: ``` public class CalcService { public int calcPrice(Trade trade, Date date) { ... } public double calcRisk(Trade trace, Date date) { ... } } ``` Should those methods be `static`?

Original source

Related problems