I came a piece of code in java where a class contains only static methods but the class itself is not declared as static. I just like to know when one will require this kind of design.
-
3It's probably a utility class. No need to create instances of it, just a bunch of static methods. – maba May 29 '13 at 08:11
-
1Only nested classes can be declared static. And it doesn't mean they don't have non static method. – Denys Séguret May 29 '13 at 08:12
-
1One such example is [Math](http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html) class. – AllTooSir May 29 '13 at 08:14
2 Answers
This idiom is called a Utility class. It is just a place where to put functions which don't naturally belong to any one class, or are excluded from dynamic dispatch for any other reason. There are several examples in the JDK, such as Math
, Collections
, and Arrays
.
You also seem to be confused about the semantics of static class
. This has nothing to do with how the class works (it is just a regular class); it has to do with the relation of the static class to its enclosing class, and actually specifies that the class has no specific relationship to it except access scoping, as opposed to a non-static (inner) class, which must be instantiated with an enclosing instance.

- 195,646
- 29
- 319
- 436
You use static methods so you don't need to instantiate the class to use them...
That's the main objective of static methods...

- 637
- 3
- 19