Those may not be equivalent.
To be able to call:
MyClass.mymethod();
mymethod
must be a static
method:
public MyClass {
public static void mymethod() { /* something */ }
}
It is true that, if mymethod
is a static
method, you could also call it like an instance method, as in:
myclass = new MyClass();
myclass.mymethod(); // works even if mymethod is static
But, in the end of the day, performance-wise (regarding the method call itself), there's no noticeable difference between the two approaches.
You should choose the approach that makes more sense semantically:
It is worth noting that, while they are not the bomb, you should try to avoid static
methods whenever possible. The more you add static
methods, the more your OO code turns procedural. Not to mention that static
methods are not overridable and, due to that, can be much harder to test/mock.