0

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.

CrazyCoder
  • 2,465
  • 8
  • 36
  • 57

2 Answers2

6

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

You use static methods so you don't need to instantiate the class to use them...

That's the main objective of static methods...

Manuel Pires
  • 637
  • 3
  • 19