Results 1 to 11 of 11
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default question about static methods...

    all,

    I found this at stackoverflow which I'm thinking can be a great generalization to the purpose of static methods:

    Java: when to use static methods - Stack Overflow

    can someone comment on this? The talk about cars and "questions", if you will, that might be asked about cars, seems like a great example to me of when you should use static methods and when you shouldn't.

    What else would one need to know regarding static methods? What I'm really looking for are things like danger points when you get into really complex programs. Obviously in a small program (say just a few classes), static methods probably don't mean anything so it wouldn't matter if methods were static or not. Does that sound right?

    thanks guys!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: question about static methods...

    Quote Originally Posted by ajetrumpet View Post
    all,
    Obviously in a small program (say just a few classes), static methods probably don't mean anything so it wouldn't matter if methods were static or not. Does that sound right?

    thanks guys!
    No! No matter how small an application is, if a method depends on a state it cannot be static. The other reply in that thread is pretty much on the point:
    Define static methods in the following scenarios only:
    If you are writing utility classes and they are not supposed to be changed.
    If the method is not using any instance variable.
    If any operation is not dependent on instance creation.
    If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
    If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: question about static methods...

    It's a fine discussion although I don't see how it can be a 'generalization' to the purpose of static methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: question about static methods...

    ph,

    can you explain to me the significance of the "state" and static methods please? forgive me, it's almost 5am over here!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: question about static methods...

    The only static in the Car example (IMO) is the mpg to kpl conversion, which is clearly a utility method...and should not be in Car anyway (as pointed out in the reply).
    The other example, of a Car comparison, simply screams "Comparator", which means it's not a static method.

    Think in terms of objects and how they interact and what their responsibilities are. Statics should (IMO) only be used after you've done all that and something simply doesn't fit neatly into the object paradigm.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: question about static methods...

    Quote Originally Posted by Tolls View Post
    Statics should (IMO) only be used after you've done all that and something simply doesn't fit neatly into the object paradigm.
    this is basically what I'm looking for. but I'm not quite sure how to generalize this into common sense English. do you? :)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: question about static methods...

    Beyond the bit PhHein quotes above, you're unlikely to find a rule.
    Dev is as much art as science.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: question about static methods...

    Quote Originally Posted by Tolls View Post
    Beyond the bit PhHein quotes above, you're unlikely to find a rule.
    Dev is as much art as science.

    yeah, and that's exactly why there aren't a lot of great products out there!! because nobody cares enough!

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: question about static methods...

    Wtf?!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    900
    Rep Power
    2

    Default Re: question about static methods...

    Well, that escalated quickly...

  11. #11
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: question about static methods...

    calm down guys. that was a joke, partially. at any rate though, i got what I wanted out of this thread. thanks! you guys are always a great help here. I'll consider this one closed.

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  5. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •