aspose file tools*
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
Author

Static vs Non-static

John Carr
Greenhorn

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: 14338
    
  22

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
Sheriff

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

Joined: Mar 31, 2011
Posts: 3

Got here by Googling performance, initially about generics. From Cristian Vrabie's posting and a bit of work http://www.coderanch.com/t/481579/Performance/java/Calls-trough-interface-references-slower#2417361 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: http://stackoverflow.com/questions/5038204/apache-commons-equals-hashcode-builder

Here the Guava solutions, or even my similar efforts from a while back (basically some easily accessible code) http://pastebin.com/tAfi8Fm3, show appropriate use of static methods in an OOP environment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static vs Non-static