10

Hi Are class methods generally measured to be faster than instance methods since it doesn't require loading an instance? If so, should we use class methods when possible? Thanks

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    Sounds like premature optimization to me. Why not measure the difference? It'll be a useful exercise for you. – artbristol Jul 01 '11 at 09:15
  • 1
    Wow, lots of answers. Check out this question and the answer I gave, dealing with the same issue. Some commenters point out that, in some circumstances (Android), static methods **are** actually faster than instance methods. http://stackoverflow.com/questions/3346764/should-java-methods-be-static-by-default – Bruno Reis Jul 01 '11 at 09:39
  • 1
    @Bruno, might be because the Android VM is not smart enough to inline method calls. This is a quite aggressive optimization. – Thorbjørn Ravn Andersen Jul 01 '11 at 10:14

9 Answers9

13

Regardless of what is faster and how much, there is one major difference that you need to remember:

  • You cannot @Override a static method!

This is very important because you essentially say that you will not, and cannot, use one of the major advantages in Java, namely overriding methods in sub-classed objects. When you call a static method, you stay with that static method and cannot override it in sub-classed objects.

Also to resolve the "which is faster" then construct a REAL test, not just a microbenchmark to investigate the actual findings. Use several JVM's to measure because JIT implementation may influence this.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • +1 for the major difference one needs to remember. If you do unit-testing, think twice before you design an application with static methods. – Axel Jul 01 '11 at 09:22
  • @Andersen: I disagree from your point. We can override a static method. There are couple of things to be kept in mind though:--- The overridden method must also be static. We cannot use the Override annotation there. – Logan Jul 28 '11 at 05:17
  • @Logan, you can provide another static method with the same signature. That is not overriding as it will not make existing code calling the old static method, magically call the new static method instead. – Thorbjørn Ravn Andersen Jul 28 '11 at 06:14
4

If a method doesn't require an instance, IMO it should be a class method. And since a class method is only possible if you don't use the instance, then your question

should we use class methods when possible

has a positive answer.

But definitely NOT for efficiency reasons

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
3

No, they are NOT faster.

However, it's a good practice to use class methods whenever that's possible, because thus you are indicating that the logic inside the method doesn't require to access any member variables.

I am saying - don't use instance methods, which can be static.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • Thanks for a good answer. But what's a good example then of what obviously can't be static? I found it hard to absolutely require instance variable since variables can be fetched from other sources. – Niklas Rosencrantz Jul 01 '11 at 13:47
  • 1
    @Niklas: If in a method, I am going to use some variables of some object, then that method must not be static. – Logan Jul 28 '11 at 05:20
2

A "class method" is available for every instance of the class and the "instance methods" juste for the current instance. So i don't see why a class method will be faster when it applies to all intances ...

Thiib
  • 39
  • 2
  • -1: I'm wondering what you mean by "a method is available to an instance" – Bruno Reis Jul 01 '11 at 09:46
  • I haven't write it, i written that "an instance method is available juste for ONE instance of the objet ( the current instance ) " – Thiib Jul 01 '11 at 09:56
  • I mean that this method "concern" juste the current instance, clear enough ? – Thiib Jul 01 '11 at 10:03
  • 1
    Not actually. Now I wonder the meaning of "concern". Could you try to use Java concepts to express what you mean? – Bruno Reis Jul 01 '11 at 10:07
2

please have a look at the link

http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

Sachin Karjatkar
  • 313
  • 3
  • 13
1

While class methods may be faster, you should definitely not write code with that way of thinking. You should use a class method when you need them. Utility classes like Arrays are a good example. Factories that return a singleton. Never use them when you require access to the internals of a class.

marchaos
  • 3,316
  • 4
  • 26
  • 35
1

When comparing class methods and instance methods, try to think of instance methods as class methods that have a extra parameter called this (In fact that is now some languages implement instance methods)

So the question becomes, "will my method be faster if it has one less parameter?" and that question does not really make sense, because the parameter list is largely irrelevant to the performance.

Try to base the decision of whether a method should be static or instance on the nature of the method, and on the data it requires, not on some premature performance benefit. Yes, performance is a feature, but it's not the only feature.

One last performance rule of thumb: Measure, measure, measure. Just because some book or article said that something should be faster, doesn't mean that it will work for you. Try it on your real-world case and back it up with empirical data.

SWeko
  • 30,434
  • 10
  • 71
  • 106
  • Actually, at the bytecode level, the difference is not only the "this" parameter that has to be pushed onto the stack before the call. The opcode used to make the call is also different. – Bruno Reis Jul 01 '11 at 09:42
  • *(In fact that is now some languages implement instance methods)* incl java :) – bestsss Jul 04 '11 at 09:11
1

From my experience, if you need to initialise an object and retain it in any sort of manner, eg using an array etc. Its best to call an instance method on that particular instance.
Theres no point calling a Class Method then passing that same instance you just initialised as an argument to that Class Method. I'm unsure on the effect during runtime, but doing this seems to be a waste (nominal or not).

I mainly use class methods for operations that dont need to be initialised, whenever I can. For example, my MathFunctions class contains all my getters to my trigonometric methods. Theres no point initialising and creating a MathFunctions object to then call an instance method simply to get an arbitrary result from one of these methods. Its easier (and faster) to simply call the class method.

So in either case, there is no "Class Method > Instance Method" or visa-versa. It simply depends on your application and what your needing. Use common sense above all, if you find yourself initialising objects for classes that hold minimal data (Eg MathFunctions) your probably better off with a Class Method.
But on the flipside, if you find yourself initialising objects then passing them into Class Methods as arguments, your most likely going to be better off with an Instance Method.

Thats my two-cents, I'm still relatively new to programming, so keep that in mind.

Ospho
  • 2,756
  • 5
  • 26
  • 39
0

I don't know about generally, but I remember measuring for some application some time ago, and static methods were indeed faster.

From a design standpoint I would argue that any method than can sensibly be static (meaning without explicitly passing an instance as parameter or something like that), should be.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 6
    Do not accept "remember measuring .. some time ago" without testing yourself. The JVM you use might be different, and/or may have been updated since. – Thorbjørn Ravn Andersen Jul 01 '11 at 10:13
  • 2
    *but I remember measuring for some application some time ago, and static methods were indeed faster.* untrue any more for long long time – bestsss Jul 04 '11 at 09:11