• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Discussion - Why Are Static Methods bad?

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

what do you think?

Thanks,

Niall
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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 .
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12186
    34
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • 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...
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Posts: 15448
    42
    Android IntelliJ IDE Java Scala Spring
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic