Questions tagged [static-methods]

98 questions
103
votes
8 answers

Why have private static methods?

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility? I would have thought an advantage to having a static method is that it can be called without an…
93
votes
11 answers

Is static universally "evil" for unit testing and if so why does Resharper recommend it?

I have found that there are only 3 ways to unit test (mock/stub) dependencies that are static in C#.NET: Moles TypeMock JustMock Given that two of these are not free and one has not hit release 1.0, mocking static stuff is not too easy. Does that…
Vaccano
  • 4,028
  • 5
  • 31
  • 37
76
votes
11 answers

Can't I just use all static methods?

What's the difference between the two UpdateSubject methods below? I felt using static methods is better if you just want to operate on the entities. In which situations should I go with non-static methods? public class Subject { public int Id…
Alexander
  • 1,159
  • 2
  • 10
  • 16
70
votes
7 answers

How to deal with static utility classes when designing for testability

We are trying to design our system to be testable and in most parts developed using TDD. Currently we are trying to solve the following problem: In various places it is necessary for us to use static helper methods like ImageIO and URLEncoder (both…
Benedikt
  • 891
  • 1
  • 6
  • 5
49
votes
2 answers

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is completely made up for the purpose of example public…
Lotok
  • 1,759
  • 2
  • 17
  • 27
38
votes
3 answers

When to use a Singleton and when to use a static class

I've searched about this here and on StackOverflow and found some differences between the two. But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to use a static class. (In languages which don't…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
34
votes
15 answers

When do 'static functions' come into use?

OK, I've learned what a static function is, but I still don't see why they are more useful than private member functions. This might be kind of a newb-ish question here, but why not just replace all private member functions with static functions…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
31
votes
5 answers

What is the difference between all-static-methods and applying a singleton pattern?

I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton pattern here or rather make all its methods static? I…
shahensha
  • 599
  • 1
  • 6
  • 15
26
votes
3 answers

Why is a private member accessible in a static method?

The following is pseudo code, I tried it in Java and PHP and both worked: class Test { private int a = 5; public static function do_test(){ var t = new Test(); t.a = 1; print t.a // 1 …
Ben
  • 1,002
  • 2
  • 10
  • 11
17
votes
2 answers

Unit testing utility classes

All of us have some utility classes, which contain only static methods, for usage from different sources. Now, there can be two approaches which can be taken towards testing this piece of code. Approach 1: Have separate unit tests for utility…
alpha_ulrich
  • 281
  • 1
  • 2
  • 6
16
votes
3 answers

Could a singleton type replace static methods and classes?

Possible Duplicate: What is the difference between all-static-methods and applying a singleton pattern? In C# Static methods has long served a purpose allowing us to call them without instantiating classes. Only in later year have we became…
Homde
  • 11,104
  • 3
  • 40
  • 68
16
votes
5 answers

Why shouldn't static methods be able to be overrideable?

In answers to this question, the general consensus was that static methods are not meant to be overridden (and thus static functions in C# cannot be virtual or abstract). This is not only the case in C#, though; Java also forbids this and C++…
16
votes
1 answer

Are we abusing static methods?

A couple of months ago I started working in a new project, and when going through the code it stroke me the amount of static methods used. Not only utility methods as collectionToCsvString(Collection elements), but also plenty of business logic…
user3748908
  • 1,607
  • 2
  • 14
  • 15
14
votes
7 answers

Is it a code smell if you are frequently creating an object just to call a method on it

I inherited a code base where there is a lot of code that goes something like this: SomeDataAdapter sda = new SomeDataAdapter(); sda.UpdateData(DataTable updateData); And then sda is never used again. Is that a code smell that indicates that those…
Shane Wealti
  • 317
  • 2
  • 12
11
votes
7 answers

Is separating most classes into data field only class and method only classes (if possible) a good or an anti-pattern?

For example, a class usually have class members and methods, eg: public class Cat{ private String name; private int weight; private Image image; public void printInfo(){ …
ocomfd
  • 5,652
  • 8
  • 29
  • 37
1
2 3 4 5 6 7