aspose file tools*
The moose likes Java in General and the fly likes New instance of a static HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "New instance of a static HashMap" Watch "New instance of a static HashMap" New topic
Author

New instance of a static HashMap

Rebecca Peltz
Greenhorn

Joined: Jul 19, 2011
Posts: 4
I have some fixed data that I'm loading into a static hash map. This data will then be manipulated and vary per user in JSP, so I create an instance of the Hashmap from the static Hashmap.



later in JSP create an instance


I just want to verify that I am truly getting an instance of the static data on the heap.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Rebecca,

That's a good question. I know you're getting a HashMap on the heap but I'm not sure if you're getting clones of the objects in the Map or references to the originals.

It is easy to test in a debugger.

You can check the refernces to the Strings.

I'm not sure it really matters as replacing items in the copy of the hash map should not affect those in the static one.

Please let us know the results if you do that experiment.

Joe


It's not what your program can do, it's what your users do with the program.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You are getting a new HashMap, which is a copy of the original HashMap. The values in the new Map are the exact Objects in the original, but that should not be a problem because the types are Integer and String, which are both immutable.

There is a problem with your terminology, though. There is no such thing as a 'static HashMap' and you are not creating an instance of it. Rather, you have a static variable which points to a HashMap and you are creating a new copy of that HashMap.


Steve
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Thanks Steve,
That cleared up my confusion. I forgot Integer and String (and all the other objects representing basic types) were immutable.

Joe
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You might do better to create an un‑modifiable Map.
Rebecca Peltz
Greenhorn

Joined: Jul 19, 2011
Posts: 4
I checked the string references in the debugger and they were different, so my method works. I like the suggested method - and didn't realize that a static could return a final instance.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2404
    
  50
I checked the string references in the debugger and they were different, so my method works

As Steve has already pointed out it wouldn't matter if the references were the same or not. Integer and String objects are immutable and so can't be edited, therefore, even if the user changes one of the values in the copy it won't affect the original in any way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New instance of a static HashMap