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

例子:

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

或:

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

我很困惑!


当前回答

不,静态方法不与实例关联;他们属于这个阶层。静态方法是第二个例子;实例方法是第一个。

其他回答

java中的静态方法属于类(而不是类的实例)。它们不使用实例变量,通常从参数中获取输入,对其执行操作,然后返回一些结果。实例方法与对象相关联,并且,顾名思义,可以使用实例变量。

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

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之前)。这给你的设计带来了很大的灵活性。此外,如果您需要状态,如果您不小心,您将以大量并发错误和/或瓶颈告终。

当您不想在代码中创建对象来调用方法时,只需将该方法声明为静态方法。因为静态方法不需要调用实例,但这里的问题是,并不是所有的静态方法都由JVM自动调用。此特权仅由main()节点享有。“public static void main[字符串…]方法,因为在运行时,这是方法Signature public“static”void main[], JVM寻求作为开始执行代码的入口点。

例子:

public class Demo
{
   public static void main(String... args) 
   {
      Demo d = new Demo();

      System.out.println("This static method is executed by JVM");

     //Now to call the static method Displ() you can use the below methods:
           Displ(); //By method name itself    
      Demo.Displ(); //By using class name//Recommended
         d.Displ(); //By using instance //Not recommended
   }

   public static void Displ()
   {
      System.out.println("This static method needs to be called explicitly");
   }
} 

输出: 这个静态方法由JVM执行 这个静态方法需要显式调用 这个静态方法需要显式调用 这个静态方法需要显式调用

静态方法应该在类上调用,实例方法应该在类的实例上调用。但这在现实中意味着什么呢?下面是一个有用的例子:

car类可能有一个名为Accelerate()的实例方法。你只能加速一个汽车,如果汽车确实存在(已经构造),因此这将是一个实例方法。

car类也可能有一个名为GetCarCount()的计数方法。这将返回创建(或构造)的汽车总数。如果没有构造汽车,这个方法将返回0,但它仍然可以被调用,因此它必须是一个静态方法。

静态的: Obj.someMethod

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