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

Posted by javanix on Stack Overflow See other posts from Stack Overflow or by javanix
Published on 2010-04-23T02:52:33Z Indexed on 2010/04/23 2:53 UTC
Read the original article Hit count: 368

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about static