Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Efficiency of static versus instance methods

Posted on 2003-11-01
5
Medium Priority
?
213 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
[X]
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
5 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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