Let's say I have a class designed to be instantiated. I have several private "helper" methods inside the class that do not require access to any of the class members, and operate solely on their arguments, returning a result.
public class Example {
private Something member;
public double compute() {
double total = 0;
total += computeOne(member);
total += computeMore(member);
return total;
}
private double computeOne(Something arg) { ... }
private double computeMore(Something arg) {... }
}
Is there any particular reason to specify computeOne
and computeMore
as static methods - or any particular reason not to?
It is certainly easiest to leave them as non-static, even though they could certainly be static without causing any problems.
One reason is that, all else being equal, static method calls should be faster. Static methods cannot be virtual, and do not take an implicit this reference.
I would like to clarify few things which other posters have said as its giving wrong information.
Firstly since the methods are private even if you declare them static you will not be able to access them outside of this class. Secondly they are private so you can not even override in subclass so static or non-static doesn't make any difference. Thirdly a non-static private method can be called from a constructor of the class also, it need not be static.
Now coming to your question if a private helper method should be defined as static or non-static. I will go with Steve's answer as marking a private method static shows that this method is stateless as I also follow this rule when I code.
Without the static modifier you cannot figure out that the method is stateless without additional analysis which can be easily done when you (re)write the method.
Then "static" modifier may give you ideas about refactoring besides other things that others may find unuseful. E.g. moving the method to some Utility class or converting it to a member method..
It might result in slightly smaller bytecode, since the static methods won't get access to
this
. I don't think it makes any difference in speed (and if it did, it would probably be too small to make a difference overall).I would make them static, since I generally do so if at all possible. But that's just me.
EDIT: This answer keeps getting downvoted, possibly because of the unsubstantiated assertion about bytecode size. So I will actually run a test.
Bytecode (retrieved with
javap -c -private TestBytecodeSize
):Invoking the static method takes two bytecodes (byteops?):
iconst_0
(for the argument) andinvokestatic
.Invoking the non-static method takes three:
aload_1
(for theTestBytecodeSize
object, I suppose),iconst_0
(for the argument), andinvokespecial
. (Note that if these hadn't been private methods, it would have beeninvokevirtual
instead ofinvokespecial
; see JLS §7.7 Invoking Methods.)Now, as I said, I don't expect there to be any great difference in performance between these two, other than the fact that
invokestatic
requires one fewer bytecode.invokestatic
andinvokespecial
should both be slightly faster thaninvokevirtual
, since they both use static binding instead of dynamic, but I have no idea if either is faster than the other. I can't find any good references either. The closest I can find is this 1997 JavaWorld article, which basically restates what I just said:But many things have changed since 1997.
So in conclusion... I guess I'm still sticking with what I said before. Speed shouldn't be the reason to choose one over the other, since it would be a micro-optimization at best.
I would declare them as static to flag them as stateless.
Java does not have a better mechanism for minor operations that aren't exported, so I think private static is acceptable.
The answer is... it depends.
If member is an instance variable specific to the object you're dealing with, then why pass it as a parameter at all?
For instance: