• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static vs Non-static

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at the back end the difference i b/w there storage
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is too difficult for "beginning Java™". Moving thread.
 
Greenhorn
Posts: 3
MySQL Database Tomcat Server C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got here by Googling performance, initially about generics. From Cristian Vrabie's posting and a bit of work https://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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic