File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can static methods cause any kind of overhead? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can static methods cause any kind of overhead?" Watch "Can static methods cause any kind of overhead?" New topic
Author

Can static methods cause any kind of overhead?

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi, that`s about it.
Can static methods cause any kind of overhead?


Leandro Melo
SCJP 1.4, SCWCD 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Actually, static methods generally cause less overhead. As polymorphism does not apply to static methods, there are no runtime checks that must be performed to determine which method to invoke.


SCJP Tipline, etc.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
I mean if this method is called a lot... i meand a lot by really a lot.
Should i build a non static method and always instantianting a object to use it (only to call the method) or should i mantain the static method?
I know that the vm only maintains one copy of each method in memory, but anyway... i don`t know exactly what should be the best option.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The static method would still have less overhead than instanciating an instance of a class to call the method. My only concern on having the static method would be whether or not there was the possibility of multiple threads calling the method. If not, then leave it static.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Originally posted by Steven Bell:
My only concern on having the static method would be whether or not there was the possibility of multiple threads calling the method. If not, then leave it static.


Actually, there might be a lot of threads accessing this method, however it`s not a method that must be thread safe (i don`t need to actually use shynchronized), because it doesn`t manipulate any instance variables (FIXED LATER: i meant class variables).

Why did you say to leave it as static only if there not too many threads accessing it?? Isn`t that the overhead i`m talking about?

Sorry, if i didn`t get the point.

[ January 27, 2005: Message edited by: Leandro Melo ]

[ January 27, 2005: Message edited by: Leandro Melo ]
[ January 27, 2005: Message edited by: Leandro Melo ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Leandro Melo:


Actually, there might be a lot of threads accessing this method, however it`s not a method that must be thread safe (i don`t need to actually use shynchronized), because it doesn`t manipulate any instance variables.[ January 27, 2005: Message edited by: Leandro Melo ]

A static method can't manipulate instance variables, even if you wanted to. However, does this method manipulate any class (static) variables?

I would agree with the above assessment that the static method will have LESS overhead than an instance method. Creating an instance of a class just to call a single method seems like a lot of overhead to me. First, there's the time need to allocate memory and call the constructor. Second, the instance takes up some amount of memory.

Layne


Java API Documentation
The Java Tutorial
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If your static method is really thread safe (be sure you're right) then lots of threads calling it a lot will be very efficient. This is how the Math methods are done.

it`s not a method that must be thread safe (i don`t need to actually use synchronized), because it doesn`t manipulate any instance variables.


Keep in mind that many operations on static variable are not thread safe.
[ January 27, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I didn't mean to only leave it static if there are only a few threads, but if there are many threads you have to be more careful. Even in the case of many threads a static method is most likely going to have less overhead. The number of threads shouldn't make a difference in that regard.

The point about the number of threads is that you have to be careful to either be sure it is thread safe, or syncronize it properly.

If there were thread safety issues I could see situations that would carry less overhead by allowing threads to have a local copy of the method, but there it is a tradeoff between the overhead of syncronizing and the overhead of mulitple instansiations. I wouldn't claim to know which would be better, that would require some analysis with a profiler.
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Originally posted by Layne Lund:

A static method can't manipulate instance variables, even if you wanted to. However, does this method manipulate any class (static) variables?
Layne


That`s what i meant, sorry for the word instance.

Thanks for your opinions.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In general, using static vs. instance methods shouldn't be decided based on performance considerations. In 99.999% the difference simply is negligible.

What you *should* think about are design considerations such as flexibility and maintainability. And regarding those properties, instance methods often win (simply because they are polymorphic).

Regarding the java.lang.Math example, that's just necessary because numbers (int, long, double etc.) simply aren't objects, and therefore can't have methods. In languages where they are (i.e. Smalltalk), those methods are typically defined as instance methods of an abstract Number base class.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can static methods cause any kind of overhead?
 
Similar Threads
Static mmbers
Static mmbers
understanding static
JSP Include
JProbe vs Eclipse profiler