1

What would be the most correct way to call a method from another class?

It is more correct to create the object:

private MyClass myclass;
myclass = new MyClass();

Then call a method:

myclass.mymethod();

Or simply call from the class:

MyClass.mymethod();

Which is more advantageous? Which is less costly and less difficult for the system to perform?

Woton Sampaio
  • 469
  • 6
  • 24

2 Answers2

4

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 mymethodis a staticmethod, 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:

  • Is mymethod an operation that makes sense only to a specific instance of a class?

    • Make it an instance (non-static) method
  • Is mymethod an operation that does not require an istance to act?

    • Make it a static method.

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.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • This is technically correct but I'd like to supply a guideline--avoid static methods as a matter of habit--think of them a little bit like public variables. They are necessary at times, but the less you have the better. – Bill K Mar 30 '18 at 23:18
  • @BillK I agree. You slowly turn your OO code in procedural the more you use `static` methods. – acdcjunior Mar 30 '18 at 23:19
  • "performance-wise (regarding the method call itself), there's no noticeable difference between the two approaches” Although this may be true in practice, in theory, the java byte code for both is different and (at least in Java 1.4<) there WAS a noticeable difference. (I may not remember the versions correctly). In any case, this is really comparing Apples to Oranges, as static methods and instance methods have different reasons to exist. – Martin Marconcini Mar 30 '18 at 23:23
  • @MartinMarconcini Thanks for the comment. Yes, the bytecode is different, I didn't enter into those details because it felt beside the point. As far as "noticeable" goes, I have seen the same method run slower when it was static versus when it instantiated an object **and** was executed as instance method. It was here in SO. Unfortunately, I can't find the post, but the situation was of a loop. It was crazy enough to teach everyone involved a lesson: static or instance, performance should not guide your decision (as you said). – acdcjunior Mar 30 '18 at 23:29
  • My (outdated) theory of why they are slower is that Static methods have to be looked upon in a table and blah blah. but I think Hotspot now optimizes this stuff more than we can possibly imagine. OTOH, static methods cannot be overridden, so… they may be inlined (?). Such a mystery. Truth be told, everybody agrees: the decision is completely NOT based upon performance here :))) – Martin Marconcini Mar 30 '18 at 23:36
  • 1
    I found this, for reference (which, to a certain degree, contradicts what I said… so maybe I should rethink my life) :) https://stackoverflow.com/questions/3805155/are-java-static-calls-more-or-less-expensive-than-non-static-calls – Martin Marconcini Mar 30 '18 at 23:38
  • 1
    @MartinMarconcini lol, now tha's some rough conclusions, haha but hotspot is da bomb, cant deny that – acdcjunior Mar 30 '18 at 23:40
  • 2
    I spent more time reading the comments as I found them more interesting than the original question - no disrespect to the OP! ;) – Mark Mar 30 '18 at 23:47
2

You don’t really get a choice. You can only do

MyClass.myMethod(); 

if the method was defined as a “class method”:

static void myMethod() {}

Those don’t act on any specific object, so you don’t provide one when you call them.

Bob Jacobsen
  • 1,150
  • 6
  • 9