aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes static vs non static Map for lockCookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "static vs non static Map for lockCookie" Watch "static vs non static Map for lockCookie" New topic
Author

static vs non static Map for lockCookie

Greg Funston
Ranch Hand

Joined: Feb 09, 2011
Posts: 81

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
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

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)


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

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

Joined: Aug 14, 2010
Posts: 98
Hey Roel,

That makes sense.

Thanks.
Matt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static vs non static Map for lockCookie