• Post Reply Bookmark Topic Watch Topic
  • New Topic

private map<..> or private final map<..> which one is better for use inside a singleton class...?  RSS feed

 
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A quick question.
private map<..> or private final map<..> which one is better for use inside a singleton class...? asking this cause I want to know, if making the map FINAL would cause any additional work for the JVM?
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anything, marking a field/variable final causes less work for the JVM, since it allows some optimizations (such as keeping the variable in a register CPU without a need to reload it from memory) to be made.

But the performance part should not be a concern for you, that would be a prime example of premature optimization (which is, of course, evil). Use final wherever it makes sense, ie. when a value of the variable won't change. Among other things, it often makes code easier to read and less error-prone, as there are fewer "moving parts".
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit chavan wrote:private map<..> or private final map<..> which one is better for use inside a singleton class...?

And further to Martin's good advice, it applies whatever type of class you're defining; and it actually makes doubly good sense if it's a singleton.

My question would be: Are you absolutely sure you want a singleton? Perhaps you haven't read this yet; or this, or this...
Mind you, there are some around who don't agree, but even Mr. Ruiz acknowledges that you need to be careful.

HIH

Winston
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Martin and thanks Winston for your replies. I will surely consider the points raised.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!