Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

static vs non static Map for lockCookie

 
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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)
 
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic