• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final variable, static method and final static method

 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a first and last name. That means two, not one. (Actually more than two is also OK, but one is not.)
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops !! I missed on that
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gouri Bargi:
Oops !! I missed on that


No problem - thanks for adding your last name!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic