aspose file tools*
The moose likes Performance and the fly likes static final variable, static method and final static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "static final variable, static method and final static method" Watch "static final variable, static method and final static method" New topic
Author

static final variable, static method and final static method

Gouri Bargi
Ranch Hand

Joined: Sep 29, 2002
Posts: 47
Hi,

I have following code:



The _logging variable was made final after a code review, after thousands of calls were written to the isLogging method. It will be a pain to replace all the method calls now - the method is called thousands of times. How much performance overhead is involved in calling an inline method as compared to using a constant? Will the compiler inline this method as it is? Do I need to declare my method final? Basically, is there any difference between a static method and a static final method?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
One way to see what the current compiler generates would be to get a bytecode viewer and use that to compare the compiler output for the various cases.

You can use Google to find one. I found jclasslib that way.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
There should be no substantial performance difference. Both can probably be called many millions of times a second. Usually this won't be an application bottleneck. Of course the best way to find out for sure is to measure the difference and report back to the forum so we can all know.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Gouri Bargi
Ranch Hand

Joined: Sep 29, 2002
Posts: 47
Thanks for the reply, Norm and Steve.

As Steve has mentioned, I realized that this will never become a bottleneck for the program. It is just that every time you see the method call in the code, you feel it could have been optimized

I checked the byte code using the jclasslib bytecode viewer. I could not make out much from the byte code, but there was no difference between the byte code generated for non-final static method and final static method. The final static method was not inlined by the compiler. I checked the JVM specifications. Regarding final methods, I found


A method can be declared final to prevent subclasses from overriding or hiding it. A private method and all methods declared in a final class (�2.8.2) are implicitly final, because it is impossible to override them. If a method is final or implicitly final, a compiler or a runtime code generator can safely "inline" the body of a final method, replacing an invocation of the method with the code in its body.


So a final method MAY be inlined, there seems to be no way you can force a method to be inlined.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Gouri,

welcome at JavaRanch!

When entering the saloon, you probably missed our naming policy which asks all of our members to use a "real sounding" first *and last* name for the display name. We don't have much rules here, but we take this one rather seriously, so please change your display name in your profile accordingly. Thanks!

Regarding your question, it probably won't make any difference. Even if in the byte code the method isn't inlined, it's quite likely that the hot spot engine will do this at runtime. But even if it doesn't it likely won't be a performance problem, so it would be wise to spend effort elsewhere instead.

By the way, if you ever want to inline a method at the source code level (for whatever reasons), a good modern IDE should contain an inline method refactoring that does this for you at the press of a mouse button - assuming that you have access to all the clients source code, of course.

Hope this helps...


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
Gouri Bargi
Ranch Hand

Joined: Sep 29, 2002
Posts: 47
Ilja,

Thanks for the reply.


When entering the saloon, you probably missed our naming policy which asks all of our members to use a "real sounding" first *and last* name for the display name. We don't have much rules here, but we take this one rather seriously, so please change your display name in your profile accordingly. Thanks!




this is my real first name. Do I need to use some other display name?


- Gouri
[ September 01, 2005: Message edited by: Gouri ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You need a first and last name. That means two, not one. (Actually more than two is also OK, but one is not.)


"I'm not back." - Bill Harding, Twister
Gouri Bargi
Ranch Hand

Joined: Sep 29, 2002
Posts: 47
Oops !! I missed on that
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Gouri Bargi:
Oops !! I missed on that


No problem - thanks for adding your last name!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static final variable, static method and final static method