Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static vs non static Map for lockCookie

 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This passage was copied from the code supplied from the SCJD Exam book by Monkhouse and Camerlengo. I did a search and got no result. Obviously the code and comment are contradictory.

/**
* A structure that keeps track of the locked Dvd records. Note that we
* have not made this static, as doing so would mean that only one set
* of reservations could exist at any given time. We rely on the class
* which uses this ReservationManager to ensure that only one instance
* exists for any reservations.
*/
private static Map<String, DvdDatabase> reservations
= new HashMap<String, DvdDatabase>();

My feeling is that this should be static as only one Map will ever be necessary to keep track of the DVD record locks but I am interested in any replies on the code and comment used here.

Thanks.

Greg Funston SCJP
 
Roel De Nijs
Sheriff
Posts: 9828
103
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on your design. If your Data class for example is a singleton, no need to mark it as static. My map to keep locked records is just an instance member, not a class member (so non-static, because otherwise my Data class would not be reusable at all)
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Roel,

I was looking through the forum as I had the same question as Greg. In your response to him, you said that your locked records map is non-static because otherwise your Data class would not be reusable. I'm not sure what you mean by saying that a static map would make your Data class not reusable. Is your static Map found in your Data class? Would you mind clarifying?

Thanks.
Matt
 
Roel De Nijs
Sheriff
Posts: 9828
103
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Pavlovich wrote:Is your static Map found in your Data class? Would you mind clarifying?

My Data class contains a map to keep the locked records, but it's not a static one, because that would make your Data class not reusable. You would have a few instances of Data class (1 for rooms, 1 for hotels, 1 for customers,...), but just 1 map with locked records (because you made it static). So if 3 different users lock records of different types but with the same record number (which is possible, because they are in different files) you'll be in trouble with just 1 single map to keep locked records.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Roel,

That makes sense.

Thanks.
Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic