我想知道什么时候使用静态方法?假设我有一个类,有几个getter和setter,一个或两个方法,我希望这些方法只能在类的实例对象上调用。这是否意味着我应该使用静态方法?

例子:

Obj x = new Obj();
x.someMethod();

或:

Obj.someMethod(); // Is this the static way?

我很困惑!


当前回答

使用静态方法有一些合理的理由:

Performance: if you want some code to be run, and don't want to instantiate an extra object to do so, shove it into a static method. The JVM also can optimize static methods a lot (I think I've once read James Gosling declaring that you don't need custom instructions in the JVM, since static methods will be just as fast, but couldn't find the source - thus it could be completely false). Yes, it is micro-optimization, and probably unneeded. And we programmers never do unneeded things just because they are cool, right? Practicality: instead of calling new Util().method(arg), call Util.method(arg), or method(arg) with static imports. Easier, shorter. Adding methods: you really wanted the class String to have a removeSpecialChars() instance method, but it's not there (and it shouldn't, since your project's special characters may be different from the other project's), and you can't add it (since Java is somewhat sane), so you create an utility class, and call removeSpecialChars(s) instead of s.removeSpecialChars(). Sweet. Purity: taking some precautions, your static method will be a pure function, that is, the only thing it depends on is its parameters. Data in, data out. This is easier to read and debug, since you don't have inheritance quirks to worry about. You can do it with instance methods too, but the compiler will help you a little more with static methods (by not allowing references to instance attributes, overriding methods, etc.).

如果你想创建一个单例,你还必须创建一个静态方法,但是…不喜欢。我的意思是,三思而后行。

更重要的是,为什么不想创建静态方法?基本上,多态被抛在了脑后。你不能重写这个方法,也不能在接口中声明它(在java 8之前)。这给你的设计带来了很大的灵活性。此外,如果您需要状态,如果您不小心,您将以大量并发错误和/或瓶颈告终。

其他回答

一个经验法则:问问自己“即使还没有构造对象,调用这个方法有意义吗?”如果是这样,它肯定是静态的。

在Car类中,你可能有一个方法:

double convertMpgToKpl(double mpg)

...这将是静态的,因为有人可能想知道35英里/加仑转换成什么,即使没有人制造过汽车。但是这个方法(设置一个特定Car的效率):

void setMileage(double mpg)

...不能是静态的,因为在构造任何Car之前调用该方法是不可想象的。

(顺便说一下,相反的情况并不总是正确的:你可能有时有一个方法涉及两个Car对象,但仍然希望它是静态的。例如:

Car theMoreEfficientOf(Car c1, Car c2)

虽然这可以转换为非静态版本,但有些人会认为,由于没有“特权”选择哪个Car更重要,所以不应该强迫调用者选择一个Car作为调用方法的对象。不过,这种情况在所有静态方法中只占相当小的一部分。

实际上,我们在类中使用静态属性和方法,当我们想要使用程序的某些部分时,它们应该一直存在,直到程序运行为止。我们知道,要操作静态属性,我们需要静态方法,因为它们不是实例变量的一部分。如果没有静态方法,操作静态属性是非常耗时的。

使用静态方法的唯一合理的地方可能是Math函数,当然main()必须是静态的,也可能是小型工厂方法。但是逻辑不应该保存在静态方法中。

使用静态方法有一些合理的理由:

Performance: if you want some code to be run, and don't want to instantiate an extra object to do so, shove it into a static method. The JVM also can optimize static methods a lot (I think I've once read James Gosling declaring that you don't need custom instructions in the JVM, since static methods will be just as fast, but couldn't find the source - thus it could be completely false). Yes, it is micro-optimization, and probably unneeded. And we programmers never do unneeded things just because they are cool, right? Practicality: instead of calling new Util().method(arg), call Util.method(arg), or method(arg) with static imports. Easier, shorter. Adding methods: you really wanted the class String to have a removeSpecialChars() instance method, but it's not there (and it shouldn't, since your project's special characters may be different from the other project's), and you can't add it (since Java is somewhat sane), so you create an utility class, and call removeSpecialChars(s) instead of s.removeSpecialChars(). Sweet. Purity: taking some precautions, your static method will be a pure function, that is, the only thing it depends on is its parameters. Data in, data out. This is easier to read and debug, since you don't have inheritance quirks to worry about. You can do it with instance methods too, but the compiler will help you a little more with static methods (by not allowing references to instance attributes, overriding methods, etc.).

如果你想创建一个单例,你还必须创建一个静态方法,但是…不喜欢。我的意思是,三思而后行。

更重要的是,为什么不想创建静态方法?基本上,多态被抛在了脑后。你不能重写这个方法,也不能在接口中声明它(在java 8之前)。这给你的设计带来了很大的灵活性。此外,如果您需要状态,如果您不小心,您将以大量并发错误和/或瓶颈告终。

我发现了一个很好的描述,当使用静态方法:

没有硬性的、写得很好的规则来决定什么时候使一个方法静态或非静态,但是有一些基于经验的观察,这不仅有助于使一个方法静态,而且还教会了什么时候在Java中使用静态方法。你应该考虑在Java中让一个方法是静态的:

If a method doesn't modify state of object, or not using any instance variables. You want to call method without creating instance of that class. A method is good candidate of being static, if it only work on arguments provided to it e.g. public int factorial(int number){}, this method only operate on number provided as argument. Utility methods are also good candidate of being static e.g. StringUtils.isEmpty(String text), this a utility method to check if a String is empty or not. If function of method will remain static across class hierarchy e.g. equals() method is not a good candidate of making static because every Class can redefine equality.

来源在这里