5

Why are all the helper classes or utility classes being declared as static?

Is it only for easyness by not creating the instanceof class everytime?

Would static hit the performace badly?

For example:

DateHelper.getCurrentDate();
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
user414967
  • 5,225
  • 10
  • 40
  • 61
  • 13
    One reason: Because they're state-less. – Hovercraft Full Of Eels Jul 07 '14 at 11:24
  • And static doesn't affect performance harmfully, au contraire. Since you don't need to create an instance, it's more efficient. – Kayaman Jul 07 '14 at 11:25
  • Static does not make a performance difference in general. In specific situations it can matter a lot. – dtech Jul 07 '14 at 11:26
  • __Not really__. See how `GSON`, on the other hand, requires you to construct `new GSON()` objects for use. There's also `ScriptManager` which uses non-static methods. – Unihedron Jul 07 '14 at 11:26
  • @Kayaman that is a very dangerous statement. Instances will still need to be made when the class is initialized. And e.g. a singleton with large memory footprint that isn't used anymore after a certain point actually degrades performance. – dtech Jul 07 '14 at 11:27
  • 1
    Its more of a *design* thing.. As Hovercroft points out, there is no need to keep the *state* here. – TheLostMind Jul 07 '14 at 11:27
  • ^ 1. See `Math.random()` constructs a `new Random()` for `private static Random randomNumberGenerator`. 2. ??? 3. Profit! – Unihedron Jul 07 '14 at 11:28
  • @dtech Well I wasn't talking about singletons. It's true that the class instance will need to be created (for any classes), but using `new MyHelper.someMethod()` instead of `MyHelper.someMethod()` would be a worse solution (for several reasons). But you're right in that static is not something that you should be using just for performance reasons. – Kayaman Jul 07 '14 at 11:29
  • 1
    @Kayaman You literally said "And static doesn't affect performance harmfully, au contraire". There are many instances where `static` hurts performance (and many were it helps performance) – dtech Jul 07 '14 at 11:31
  • @dtech - I'm trying to recall, from my 10 years of working inside a JVM, a situation where making a method static made it less efficient. – Hot Licks Jul 07 '14 at 11:40
  • Yes, I'd appreciate if @dtech elaborated a bit on this, since he seems to know many instances where static hurts performance. Preferably something that doesn't involve a design flaw (such as the singleton with a long living static instance). Give me an example where a static method performs worse than a non-static one. Or are you just arguing about my wording? – Kayaman Jul 07 '14 at 11:42
  • @HotLicks `static` can also apply to attributes, then it can become dangerous. I think you're right about that making a *method* static will not hurt performance, but that doesn't mean making *something* static won't hurt performance. Your statement that "static won't hurt performance" was too general. – dtech Jul 07 '14 at 11:43
  • @Unihedron - ??? A static method is not "inner", and is loaded and verified exactly like an instance method. – Hot Licks Jul 07 '14 at 11:44
  • @dtech - I didn't say "static won't hurt performance", and the person who did was clearly saying it in a context where the term was meant to apply to methods. (And how do you apply `static` to an "attribute"?) – Hot Licks Jul 07 '14 at 11:47
  • @HotLicks How is "static won't hurt performance" different from "static doesn't affect performance harmfully" then? And to apply static to a field/attribute... Put static in its declaration `public static int myField = 5;` – dtech Jul 07 '14 at 11:51
  • @dtech - Just a little hint: After each comment is the ID of the person commenting. And where in the JLS is the term "attribute" defined? – Hot Licks Jul 07 '14 at 11:58
  • @HotLicks excuse me, I meant to react to Kayaman. Attribute is [a common synonym for field](http://stackoverflow.com/questions/11350423/terminology-of-class-attribute-vs-member-vs-variable-vs-field). – dtech Jul 07 '14 at 12:10
  • So you're nit-picking others' terminology but want leeway with yours? – Hot Licks Jul 07 '14 at 12:15

2 Answers2

7

Because they are not confined to state, they have a single functionality that is purely stateless. For example Math.abs(double a) , it takes a double argument and returns the absolute value. As simple as that. So you dont have to do for example something like

Math m=new Math();
m.abs(12.33);

each time to do a simple absolute value calculation which depends only on its argument, using static imposes a simplicity to calling utility methods.

Edit:- Want to add that there is no performance hit in using static methods. There would only be a lag if the method is static synchronized and mutiple threads want to access it simultaneuously, vs accessing same method (in non static context) in different instances by threads. But most utility methods are atomic in functionality so making them synchronized does not make sense.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
-1

To not to create their instances. Also you can make them follow singleton pattern

Meiblorn
  • 2,522
  • 2
  • 18
  • 23