tags:

views:

171

answers:

5

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().

+2  A: 

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

SHiNKiROU
+10  A: 

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.

Bakkal
+5  A: 

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.

TofuBeer
+2  A: 

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(); }
Niels Wind
+4  A: 

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...)

Ash
You do not override static methods, you hide them. Overriding implies polymorphism, which you don't get with static methods.
TofuBeer
@TofuBeer thanks, I wasn't precise enough in my terminology. From the perspective of someone using `this` on a static method, it might imply some sort of intention for polymorphism, which isn't going to work.
Ash