This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Variable Access Question

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a singleton class (only one instance ever) is it more efficient to access a variable directly rather than using setter/getters? Or is the difference sufficiently insignificant that getters/setters are still better programming practice? TIA.
 
Marshal
Posts: 6960
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May you show an example?
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example of direct variable access.

 
Liutauras Vilda
Marshal
Posts: 6960
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for an example.

It is a good practice to prevent users from fiddling with variables directly and introduce methods (i.e. getters/setters) to access them.

In your given example or more precisely in your application it may make no difference, however, if you were to use such singleton object in concurrent environment, you wouldn't have the way to synchronise reading/writing.

Can research more starting form here.
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really something you should be even considering. First of all, it's almost impossible to make the choice without prematurely optimizing the code - optimizing based on what you think will be efficient, instead of what is measurably efficient. Because it's such a low-level function, if you chose wrong, the cost of re-coding is likely to be prohibitive,

Fortunately, neither case would make much performance difference, The real performance boosts don't come from low-level twiddling, they come from good algorithms. The most highly-optimized bubble search in the world gets left in the dust by a heapsort when the conditions are right (and vice versa!).

Also, the Java compiler and runtime are heavily optimizing. Just because you coded a method call doesn't mean that Java cannot recognize the simplicity of the operation and internally translate it to a direct fetch/store.

I recommend accessor methods, though. They're easier to temporarily attach debugging and profiling extensions to.
 
Sam Ritter
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the information I was looking for. Although a bit counter intuitive, I guess that is why using setters/getters is considered good programming practice. Thanks.
 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!