permaculture playing cards
The moose likes Performance and the fly likes Static vs Non-static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Static vs Non-static" Watch "Static vs Non-static" New topic

Static vs Non-static

John Carr

Joined: Jan 16, 2010
Posts: 17
What are the differences between static and non-static performance wise?

Essentially, what I'm trying to figure out is what exactly is different between static and non-static allocations, and the performance differences between them. (I know what it does on the surface, but I'm looking for what it does behind the scenes)
swapnil kataria
Ranch Hand

Joined: Feb 26, 2011
Posts: 64
at the back end the difference i b/w there storage
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

There is no significant performance difference between calls to static and non-static methods.

In theory, static method calls might be faster, but the JVM optimizes method calls heavily, so this theoretical difference will most likely not exist in practice.

The theoretical difference is that non-static methods are late bound - the JVM has to figure out at runtime where the code is exactly that needs to be called, because non-static methods might be overridden in subclasses. Static methods are not subject to late binding, so at compile time it's already known what code is to be called exactly.

In practice, the JVM does some very sophisticated things; it analyzes the class hierarchy and converts dynamic method calls (to non-static methods) to static method calls automatically if it can, so that there is no difference at all anymore between non-static and static method calls.

Anyway, do not modify your program to use static methods instead of non-static methods just because you think it helps performance. Doing so is premature optimization. You'd be compromising the design of your application based on some vague feeling that it might help performance. You should only optimize based on evidence - first, determine if your program actually has a performance problem, and if it does, measure with a profiler to find out what the bottleneck is, and concentrate on improving that.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
I think this is too difficult for "beginning Java™". Moving thread.
Martin Humby

Joined: Mar 31, 2011
Posts: 3

Got here by Googling performance, initially about generics. From Cristian Vrabie's posting and a bit of work can now comment:

1) Call + return time for static vs. dynamic calls is strongly platform and JVM dependent

2) JVM optimization can be faster than native code for some calls (tested under Windows XP), which came as a complete surprise to me.

Oh yes, for most purposes the way a call is dispatched is insignificant (particularly if any user action is involved). Often calling time is negligible compared with time to execute code in the method body. With fast modern hardware most performance bottlenecks tend to be thread blocking and/or DB related, although maybe not for smartphones. Initial choice of appropriate architecture and data structures is not premature optimization and even if everything has been designed correctly it is still possible to compromise performance so an application does not scale well by choosing complex OOP calls over static methods:

Here the Guava solutions, or even my similar efforts from a while back (basically some easily accessible code), show appropriate use of static methods in an OOP environment.
I agree. Here's the link:
subject: Static vs Non-static
It's not a secret anymore!