Is there a rule of thumb for when to code a static

2019-03-13 16:18发布

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条回答
再贱就再见
2楼-- · 2019-03-13 16:51

Basically, the rule of thumb is if it uses any data specific to the object, instance. So Math.max is static but BigInteger.bitCount() is instance. It obviously gets more complicated as your domain model does, and there are border-line cases, but the general idea is simple.

查看更多
做自己的国王
3楼-- · 2019-03-13 16:51

This thread looks relevant: Method can be made static, but should it? The difference's between C# and Java won't impact its relevance (I think).

查看更多
Deceive 欺骗
4楼-- · 2019-03-13 17:00

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.

查看更多
爷、活的狠高调
5楼-- · 2019-03-13 17:02

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.

:)

查看更多
叼着烟拽天下
6楼-- · 2019-03-13 17:03

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.

查看更多
小情绪 Triste *
7楼-- · 2019-03-13 17:05

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.

查看更多
登录 后发表回答