Solved

Efficiency of static versus instance methods

Posted on 2003-11-01
5
209 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 91
HSSFWorkbook cannot be resolved error 10 69
jar file executable 12 52
jsp insert to database example 2 23
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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