• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing static fields  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question... I am using Eclipse to develop my java apps in. I recently changed one of the options in the IDE to output a warning if it came across any code with indirect access to a static member. There are TONS of places that I am doing this. I guess my question is... is there any performance hit if I indirectly access a static memeber vs. directly accessing it?

Example:
I am currently using the following code:


Eclipse suggests:


Thanks!
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jennifer,

You may want to try running some tests, such as accessing a series of different static variables indirectly and directly and timing the difference.

However, my intuition tells me that there is no performance difference. A static variable is loaded into memory when the class is loaded. So once it is in memory, it should not matter how you access it. From reading some of Eclipse's documentation, it said something about having issues with indirect access before JDK 1.2, but I'd have to assume that is not your situation.

If it really bothers you, maybe eclipse has some auto-fix for all of those, but I wouldn't concern yourself with it.

Hope this helps,
Jeff
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DISPOSE_ON_CLOSE, among many others, is a compile-time constant. That means that its value will be compiled directly into any class that uses it; therefore you can say with 100% certainty that the way you access the value doesn't matter at all, as it won't lead to any code being generated at all.

For actual static variables -- nonfinal ones, or ones with values determined at runtime -- the class you use to name the variable is compiled into the code, so that there may indeed be a very, very tiny penalty for naming statics using a reference or name of a derived type.

Using a reference of the same type as the class has no penalty associated with it, but it's considered bad form.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!