3

I was going through java class in which I found all the methods were static , I want to know when there is the requirement or where the condition arises when we have to prefix static in front of all the methods. Is it any kind of design pattern..?

Crazy4Java
  • 269
  • 2
  • 6
  • 13

4 Answers4

4

This is typically used in utilities classes. Think for example the Math class. You don't need an instance of an object to calculate the minimum of 2 numbers, so it makes sense that Math.min is a static method.

However, overuse of static methods / fields is not necessarily a good design practice.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Exactly: utility pattern

http://en.wikipedia.org/wiki/Utility_pattern

tgoossens
  • 9,676
  • 2
  • 18
  • 23
0

Helper classes usually provide static only methods. These are classes that provide some methods that are not specific just to one kind of object, but can be shared across the entire project. For instance, a MathHelper could define a method for calculating the average of an array of float values, another one for calculating the distance between 2 points and so on.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

Classes which have all static methods are used for below purposes :

1) Copied from Joshua Bloch Effective Java

Interfaces can’t have static methods, so by convention, static factory methods for an interface named Type are put in a noninstantiable class (Item 4) named Types. For example, the Java Collections Framework has thirty-two convenience implementations of its collection interfaces, providing unmodifiable collections, synchronized collections, and the like. Nearly all of these implementations are exported via static factory methods in one noninstantiable class (java.util.Collections). The classes of the returned objects are all nonpublic.

2) Utility Pattern as suggested by @tgoossens

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72