• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New instance of a static HashMap

 
Rebecca Peltz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do better to create an un‑modifiable Map.
 
Rebecca Peltz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic