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

例子:

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

或:

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

我很困惑!


当前回答

我想知道什么时候使用静态方法?

静态方法的一个常见用途是访问静态字段。 但是你可以有静态方法,而不需要引用静态变量。不引用静态变量的Helper方法可以在一些java类中找到,如java.lang. math 公共静态int min(int a, int b) { 返回(a <= b) ?A: b; } 另一个用例,我认为这些方法与同步方法相结合是在多线程环境中实现类级锁定。

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

如果你需要访问类的实例对象上的方法,你的方法应该是非静态的。

Oracle文档页提供了更多详细信息。

不是所有的实例和类变量和方法的组合都被允许:

实例方法可以直接访问实例变量和实例方法。 实例方法可以直接访问类变量和类方法。 类方法可以直接访问类变量和类方法。 类方法不能直接访问实例变量或实例方法——它们必须使用对象引用。而且,类方法不能使用this关键字,因为没有this的实例可以引用。

其他回答

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

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

double convertMpgToKpl(double mpg)

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

void setMileage(double mpg)

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

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

Car theMoreEfficientOf(Car c1, Car c2)

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

当您希望能够在没有类实例的情况下访问方法时,请使用静态方法。

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

没有硬性的、写得很好的规则来决定什么时候使一个方法静态或非静态,但是有一些基于经验的观察,这不仅有助于使一个方法静态,而且还教会了什么时候在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.

来源在这里

可以使用静态方法,如果

One does not want to perform an action on an instance (utility methods) As mentioned in few of above answers in this post, converting miles to kilometers, or calculating temperature from Fahrenheit to Celsius and vice-versa. With these examples using static method, it does not need to instantiate whole new object in heap memory. Consider below 1. new ABCClass(double farenheit).convertFarenheitToCelcium() 2. ABCClass.convertFarenheitToCelcium(double farenheit) the former creates a new class footprint for every method invoke, Performance, Practical. Examples are Math and Apache-Commons library StringUtils class below: Math.random() Math.sqrt(double) Math.min(int, int) StringUtils.isEmpty(String) StringUtils.isBlank(String) One wants to use as a simple function. Inputs are explictly passed, and getting the result data as return value. Inheritence, object instanciation does not come into picture. Concise, Readable.

注意: 很少有人反对静态方法的可测试性,但是静态方法也可以被测试!使用jMockit,可以模拟静态方法。可测试性。在下面的例子:

new MockUp<ClassName>() {
    @Mock
    public int doSomething(Input input1, Input input2){
        return returnValue;
    }
};

静态的: Obj.someMethod

当你想要提供对方法的类级访问时,使用static,即在没有类实例的情况下,方法应该是可调用的。