Is there a rule of thumb for when to code a static method vs an instance method?

前端 未结 13 1505
暖寄归人
暖寄归人 2021-02-04 17:19

I\'m learning Java (and OOP) and although it might irrelevant for where I\'m at right now, I was wondering if SO could share some common pitfalls or good design practices.

相关标签:
13条回答
  • 2021-02-04 18:05

    IMHO, if you can make it a static method (without having to change it structure) then make it a static method. It is faster, and simpler.

    If you know you will want to override the method, I suggest you write a unit test where you actually do this and so it is no longer appropriate to make it static. If that sounds like too much hard work, then don't make it an instance method.

    Generally, You shouldn't add functionality as soon as you imagine a use one day (that way madness lies), you should only add functionality you know you actually need.

    For a longer explanation...

    http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It

    http://c2.com/xp/YouArentGonnaNeedIt.html

    0 讨论(0)
  • 2021-02-04 18:10

    I don't think any of the answers get to the heart of the OO reason of when to choose one or the other. Sure, use an instance method when you need to deal with instance members, but you could make all of your members public and then code a static method that takes in an instance of the class as an argument. Hello C.

    You need to think about the messages the object you are designing responds to. Those will always be your instance methods. If you think about your objects this way, you'll almost never have static methods. Static members are ok in certain circumstances.

    Notable exceptions that come to mind are the Factory Method and Singleton (use sparingly) patterns. Exercise caution when you are tempted to write a "helper" class, for from there, it is a slippery slope into procedural programming.

    0 讨论(0)
  • 2021-02-04 18:13

    Your default choice should be an instance method.

    0 讨论(0)
  • 2021-02-04 18:16

    One important thing to remember is that static methods cannot be overridden by a subclass. References to a static method in your code essentially tie it to that implementation. When using instance methods, behavior can be varied based on the type of the instance. You can take advantage of polymorphism. Static methods are more suited to utilitarian types of operations where the behavior is set in stone. Things like base 64 encoding or calculating a checksum for instance.

    0 讨论(0)
  • 2021-02-04 18:18

    I would use an instance method by default. The advantage is that behavior can be overridden in a subclass or if you are coding against interfaces, an alternative implementation of the collaborator can be used. This is really useful for flexibility in testing code.

    Static references are baked into your implementation and can't change. I find static useful for short utility methods. If the contents of your static method are very large, you may want to think about breaking responsibility into one or more separate objects and letting those collaborate with the client code as object instances.

    0 讨论(0)
  • 2021-02-04 18:20

    If you keep state ( a value ) of an object and the method is used to access, or modify the state then you should use an instance method.

    Even if the method does not alter the state ( an utility function ) I would recommend you to use an instance method. Mostly because this way you can have a subclass that perform a different action.

    For the rest you could use an static method.

    :)

    0 讨论(0)
提交回复
热议问题