开发者

Why is it preferable to call a static method statically from within an instance of the method's class?

If I create an instance of a class in Java, why is it preferable to call a static method of that same class statically, rather than using this.method()?

I get a warning from Eclipse when I try to call static method staticMethod() from within the custom class's constructor via this.staticMethod().

public MyClass() { this.staticMethod(); }

vs

public MyClass() { MyClass.staticMethod(); }

Can anyone explain why this is a bad thing to do? It seems to me like the compiler should already have allocated an instance of the object, so statically allocating memory would be开发者_开发技巧 unneeded overhead.

EDIT:

The gist of what I'm hearing is that this is bad practice mainly because of readability, and understandably so. What I was really trying to ask (albeit not very clearly) was what differences there are at 'compilation', if any, between calling MyClass.staticMethod() or this.staticMethod().


Static methods are not tied to an instance of the class, so it makes less sense to call it from a this than to call it from Class.staticMethod(), much more readable too.


MyClass.staticMethod() makes it clear that you are calling a static (non-overrideable) method.

this.staticMethod() misleads the reader into thinking that it is an instance method.

staticMethod() is also on the misleading side (though I normally do it that way).

If you think of people reading your code as unfamiliar with it you tend to try to make the code clearer, and this is a case where the code is clearer by having ClassName.method instead of instance.method.


In addition to the other answers which have mentioned making it clear you're using a static method, also note that static methods are not polymorphic, so being explicit with the class name can remove any confusion as to which method is going to be called.

In the code below, it's not entirely obvious that b.test() is going to return "A" if you're expecting the polymorphism of a non-static method:

public class TestStaticOverride
{
  public static void main( String[] args )
  {
    A b = new B();
    System.out.println( "Calling b.test(): " + b.test() );
  }

  private static class A
  {
    public static String test() { return "A"; }
  }

  private static class B extends A
  {
    public static String test() { return "B"; }
  }
}

If you change the code to B b = new B(); it will print out "B".

(Whether it's ever a good idea to "override" static methods is probably a discussion for another day...)


Static methods are really not part of your instance - and it will not be able to access any of your instance variables anyway, so I would dare thinking it doesn't make a lot of sense calling it from the constructor.

If your need to initialize static objects use

private static List l = new ArrayList();   static { l.add("something"); }

If you still need to call it its perfectly legal to call local static methods without prefixing your local class name, like this (no eclipse warning)

public MyClass() { staticMethod(); }


Because this. normally reference to instance methods, therefore, it's a bad idea to do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜