Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Efficiency of static versus instance methods

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 63
Windows 10 IE Certificate Issue 10 51
runtime exception 2 41
jsp login check 12 42
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

856 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