Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Efficiency of static versus instance methods

Posted on 2003-11-01
5
Medium Priority
?
215 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
3 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 136 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 132 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 132 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

877 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