Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static vs Non-static

 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the back end the difference i b/w there storage
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is too difficult for "beginning Java™". Moving thread.
 
Martin Humby
Greenhorn
Posts: 3
C++ MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic