aspose file tools*
The moose likes Java in General and the fly likes Discussion - Why Are Static Methods bad? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Discussion - Why Are Static Methods bad?" Watch "Discussion - Why Are Static Methods bad?" New topic
Author

Discussion - Why Are Static Methods bad?

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
Hi,

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

what do you think?

Thanks,

Niall
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Mar 28, 2012
Posts: 859

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

Joined: Jul 19, 2010
Posts: 147

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.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

    Joined: Oct 02, 2003
    Posts: 11418
        
      16

    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...


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14270
        
      21

    Similar discussion from not so long ago: Using too much static functions in code


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Discussion - Why Are Static Methods bad?