开发者

Class methods vs instance methods

开发者 https://www.devze.com 2023-03-17 03:04 出处:网络
Hi Are class methods generally me开发者_C百科asured to be faster than instance methods since it doesn\'t require loading an instance? If so, should we use class methods when possible?

Hi Are class methods generally me开发者_C百科asured to be faster than instance methods since it doesn't require loading an instance? If so, should we use class methods when possible? Thanks


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.


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


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.


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


please have a look at the link

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


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号