Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default final HashMap hm=new HashMap();

    final HashMap hm=new HashMap();
    public void show()
    {
    hm.put("name",value);
    }
    in this prg here the final hashtable value can be changed in
    put method,its the prg run?

    If run why?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The reference is final, not the underlying storage. Making the hashmap final doesn't make it unmodifiable, instead it makes it so you can't re-assign the variable to a new hashmap. If you did
    Java Code:
    hm = new HashMap();
    In the show method you would run into problems.

    The hashmap itself is a mutable object; the reference to it is final.

    There are methods on the map class to get an unmodifiable view of the hashmap, check the API.

    Jos beat me to it while editing my post to add the api
    Last edited by sunde887; 07-21-2011 at 09:34 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Also, read the API documentation for the Collections utility class if you want an unmodifiable Map.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Interesting concept: unmodifiable Map.
    Or is that after putting something in it?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Interesting concept: unmodifiable Map.
    Or is that after putting something in it?
    No, you can only make an empty Map unmodifiable ;-)

    kind regards,

    Jos (<--- silly ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. HashMap help
    By BobswYourUcle in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 09:05 PM
  2. Should i use a hashmap?
    By 6Sloth9 in forum New To Java
    Replies: 7
    Last Post: 05-02-2011, 02:38 AM
  3. Something like HashMap
    By BigBear in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 11:08 AM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. HashMap Help
    By ScaryJello in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •