• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New instance of a static HashMap

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might do better to create an un‑modifiable Map.
 
Rebecca Peltz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic