• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Discussion - Why Are Static Methods bad?

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

a fellow developer recently told me that static methods are bad,

what do you think?

Thanks,

Niall
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing wrong with static methods in the right place. I use them regularly. What I would say, though, is that overuse of static methods is sometimes a sign that the programmer is really writing procedural code in Java, and isn't working in an object-oriented way.

In my opinion, obviously .
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on their usage.

In some cases yes, in others not so much. A lot of the internal Math functions for instance are static.

WP
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read this somewhere, too, and since then, I'm really paranoid about using static methods. I'm working on a fairly large (web-) project and noticed that many tasks can be done by static methods (such as file I/O, some simple mathematical functions, parsing strings, find and instantiating classes for me) - but others can not.
I'm using my best judgment and don't go out of my way to force methods that could be static not to be, but everytime I add a new static method to a class (there are classes in my project which have only static methods) I get a bad feeling.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Niall Loughnane wrote:a fellow developer recently told me that static methods are bad,
what do you think?


He's wrong. Static methods are what they are: static. In the right situation, they are exactly what you need; and also probably slightly faster than non-static ones.

The question then is: when is the right time to use static methods? A couple I can think of:
  • Utility classes: There are several examples in the standard API, a few of which have already been listed, but to add a few: java.util.Arrays, java.util.Collections, and for v7, java.nio.file.Files.
  • Factories: A very good use for static methods: Examples: String.valueOf() (in fact most valueOf() methods), Integer.parseInt().

  • Other than that: what Matthew said.

    Winston
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Every single time someone makes an absolute statement like this, they are dead wrong.



    Bu seriously...in programming, I don't believe there are absolutes. It may be correct that MOST of the time static methods are bad...but that is because most developers don't understand the right time, place, and way to use them, so they get abused.

    You'll hear people say that the singleton is terrible, and should never be used...but again, in the right time and place, it is the perfect solution.

    The trick is learning where and when that time is...
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Similar discussion from not so long ago: Using too much static functions in code
     
    On top of spaghetti all covered in cheese, there was this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic