Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Methods All Static in Class

Tags:

c#

I was told by my colleague based on one of my classes (it is an instance class) that if you have no fields in your class (backing fields), just make all methods static in the class or make the class a singleton so that you don't have to use the keyword new for calling methods in this BL class.

I assume this is common and good practice? Basic OOP? I just want to see people's opinion on that.

I think basically he's saying since there's no state, no need for the methods to be instance methods.

I'm not sure about making it a singleton every time as an option in this case...is that some sort of pattern or good advice he's giving me?

Here's the class I'm talking about (please do not repost any of this code in this thread, this is private): http://www.elbalazo.net/post/class.txt

like image 411
PositiveGuy Avatar asked Sep 29 '09 17:09

PositiveGuy


People also ask

Can all methods of a class be static?

If you have no class level variables, then yes, you can make all the methods on your class static.

Should you make all methods static?

Static methods are usually preferred when: All instance methods should share a specific piece of code (although you could still have an instance method for that). You want to call method without having to create an instance of that class. You must make sure that the utility class is never changed.

Can all methods be static in Java?

Java has both static and non-static methods. Static methods are class methods, and non-static methods are methods that belong to an instance of the class. Static methods can access other static methods and variables without having to create an instance of the class.

Can we have 2 static methods in a class?

The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters.


2 Answers

There is very little downside to calling new and constructing a class reference, especially if the class has no state. Allocations are fast in .NET, so I wouldn't use this alone as a justification for a class to be static.

Typically, I feel a class should be made static if the class has no specific context - if you're using the class just as a placeholder for "utility" methods or non-context specific operations, then it makes sense to be a static class.

If that class has a specific need for context, and a meaning in a concrete sense, then it probably does not justify being static, even if it has no state (although this is rare). There are times where the class purpose is defined by its reference itself, which provides "state" of a sort (the reference itself) without any local variables.

That being said, there is a big difference between a static class and a singleton. A singleton is a different animal - you want to use it when you need an instance, but only one instance, of the class to be created. There is state in a singleton, but you are using this pattern to enforce that there is only a single copy of the state. This has a very different meaning, and I would highly recommend avoiding using a singleton just to prevent needing to "call new".

like image 109
Reed Copsey Avatar answered Nov 12 '22 09:11

Reed Copsey


There's no absolute rule for when a class should be static. It may have no state, but you may need it for reference equality or locking. Classes should be static when their purpose fits it being implemented as a static class. You shouldn't follow hard-and-fast rules in these situations; use what you 'feel' is right.

Having no state makes it a candidate for static-ness, but look at what it's being used for before arbitarily refactoring it.

like image 28
thecoop Avatar answered Nov 12 '22 09:11

thecoop