They had given one progam
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?
Answers were Sorted based on User's Feedback
Answer / kr
hash map values can be modified but the hashmap object
cannot be modified
hm.put("name",value); //allowed
hm = new HashMap() // throw error since the object is fianl
| Is This Answer Correct ? | 19 Yes | 1 No |
Answer / abhinav mutreja
Thats Correct that contents of the HashMap can be changed
even if HashMap is declared as final.
If you contents also should not get changed then you can use
Map hm = Collections.unmodifiableMap(new HashMap());
here if you do
hm.put(1,"Hi");
then it would give Runtime Exception :-
Exception in thread "main"
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put
(Unknown Source)
| Is This Answer Correct ? | 8 Yes | 0 No |
What is public static?
Is java call by value?
Difference between String & StringBuffer
16 Answers IBM, Infosys, Tech Mahindra, Wipro,
What does super()represent, and how is it used in Java?
what is the difference between equals method and ==
17 Answers IBM, Professional Access, TCS,
What is == in java?
write a class to input 2 points by the user & check the line maked by the points is horizontal,vertical or rectangle?
How to print an arraylist in java?
1) There are 10 different threads in runnable state. Each having priority 1 to 10. How does the CPU schedules or executes these threads?
How GC (Garbage Collector) knows the objects reference is unused.Whether GC removes the unused object Parmanently or it maintains something.
What are the differences between string, stringbuffer and stringbuilder?
Where are variables stored?