21

I have a utility class which has non static methods with no instance variables. So I am thinking of converting all the methods to static methods. I doubt there will be any memory or performance impacts. But I just wanted to confirm.

Will changing such a method to be a static have any performance impact on the program?

0

7 Answers 7

25

One final thing to add to what people have said here.

Using a static method has a slightly less overhead due to the fact that you have guaranteed compile time binding. Static method calls will create the bytecode instruction invokestatic. ]

In a typical scenario, instance methods are bound at runtime, and will create the bytecode instruction invokevirtual which has higher overhead than invokestatic.

However, this only becomes relevant in the case of likely millions of iterations, and i would caution against this driving your class design. Do what makes sense from a design perspective. Based on your description, static methods are probably the way to go. In fact, this is relatively standard practice to create a utility class:

public class MyUtilities {
   private MyUtilities() { } // don't let anyone construct it.
   public static String foo(String s) { ... }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The story is a bit more complicated since invokevirtual is not an actual machine instruction. In the worst case it will be implemented as a monomorphic call site, but if the class is final (or effectively final by virtue of private constructor), it will be a hardwired call. Even without that, the runtime may notice that there are no subclasses and hardwire the call anyway.
Yes, i understand it doesn't guarantee that it's not hardwired, but i was trying to keep the explanation simple, as most people don't understand the internals of the jvm.
I must say your wording is misleading. There is no "typical scenario" here: instance method invocation always compiles to invokevirtual; likewise with static method and invokestatic.
19

EDIT: Addressing the performance aspect: it's cheaper not to have to create an instance of something pointlessly, but the difference is very likely to be completely irrelevant. Focusing on a clear design is much more likely to be important over time.

Utility methods are frequently static, and if all the methods within a class are static it may well be worth making the class final and including a private constructor to prevent instantation. Fundamentally, with utility classes which don't represent any real "thing" it doesn't make logical sense to construct an instance - so prevent it.

On the other hand, this does reduce flexibility: if any of these utility methods contain functionality which you may want to vary polymorphically (e.g. for testing purposes) then consider leaving them as instance methods - and try to extract some meaningful class name to represent the "thing" involved. (For example, a FooConverter makes sense to instantiate - a FooUtil doesn't.)

2 Comments

+1 for the second paragraph. If static methods are used, then it wouldn't be possible to swap the implementation.
@MarkoTopolnik: Have added a section at the start of the answer.
4

There are two requirements that must be met for a method to be eligible for conversion into static:

  1. no instance variables accessed (this is met in your case);
  2. will never need to be subject to overriding (for this you may have to think it through).

However, when these requirements are met, it is actually recommended to make the method static because it narrows down the context the method is run within.

Finally, note that there are no performance issues to talk about here and any theoretical difference is in fact in favor of static methods since they don't involve dynamic method resolution. However, instance method invocation is blazing fast in any relevant JVM implementation.

As far as memory, the story is the same: a theoretical difference is in favor of the static method, but there is no practical difference if compared against a singleton utility class.

Comments

3

If the utility class is not subclassed, converting methods that do not access the instance variables to static is a good idea. You should go through the code and convert invocations to static syntax, i.e.

int res = utilityInstance.someMethod(arg1, arg2);

should be converted to

int res = UtilityClass.someMethod(arg1, arg2);

for clarity.

There will be no noticeable performance impact: although theoretically static invocations are slightly less expensive, the difference is too small to consider important in most scenarios.

Comments

1

It is common for utility classes without state(like java.lang.Math for example) to have public static methods. This way you don't need to create an instance of the class to use it.

Comments

0

Static method good idea when you are going to use the particular functionality very often.

Comments

-2

The difference is that you need an instance in order to use them, so the user has to make an instance which will be a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.