Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

static

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

like image 515
javanix Avatar asked Apr 23 '10 02:04

javanix


People also ask

What is the preferred way to call a static method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

Why should you use a static method?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

When should you use a static method when you want your method to be available?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.

Can you use a static method on an instance?

Instance method vs Static methodStatic methods can't access instance methods and instance variables directly. They must use reference to object. And static method can't use this keyword as there is no instance for 'this' to refer to.


2 Answers

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.

like image 200
bakkal Avatar answered Oct 04 '22 01:10

bakkal


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.

like image 27
TofuBeer Avatar answered Oct 04 '22 02:10

TofuBeer