Solved

Efficiency of static versus instance methods

Posted on 2003-11-01
5
207 Views
Last Modified: 2010-03-31
I have a general question as to the efficiency of static versus instance methods: could an increase in efficiency/speed be realized by declaring a method static or not.  Specifically, if it's more convenient to use an instance method, but I could concievably re-write it as a static method, would there be a gain?  My problem results from not being able to declare static methods in an interface -- and so I am using instnace methods.  But if I can write a method as static, should I?  For full points, please offer some why/how as to the reason there is or is not a price to pay for a method being static or not.

Thanks.
0
Comment
Question by:sapainca
5 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 34 total points
ID: 9663940
hm..
1) i think you should never use static methods for optimization your programm. It will only make your design tangled, ant you won't get so greate perfomance growth. If you are about perfomance, do it other way.
   Use static methods according to your application design. If you need method per class basis, instead of object.

2) i don't know exatcly how static methods implements within JVM. But in C++, static methods is executed by simply call function at specified addres. And virtual function called inderectly through VirtualTable, so there you loose some only some time for inderection   call. And that's all.
      I think the same in Java.
  By the way...you can easily check it. just create a benchmark, with static methods, and the same without.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 33 total points
ID: 9664038
Occasionally methods can be optimized by being declared static, and further so, by being declared final. This should only be done for key parts of the program and not done throughout and it leads to procedural styles of programming. Obviously finals could not be overridden either.
0
 

Assisted Solution

by:sasjaa
sasjaa earned 33 total points
ID: 9678557
It is a common statement that you should optimize your algorhythm and not your code. The compiler can usually do a better job ad the optimization that you choose could actually force the compiler into choosing a less than optimal solution, thus SLOWING YOU DOWN. Choose the way of doing something that makes the code easier to understand and maintain and any optimization to the algorhythm will be easer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now