Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Accessing static fields

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?

I am currently using the following code:

Eclipse suggests:

Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • 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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!