aspose file tools*
The moose likes Beginning Java and the fly likes Why final is not applied for Map ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why final is not applied for Map ?" Watch "Why final is not applied for Map ?" New topic
Author

Why final is not applied for Map ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

Map hm= new HashMap();
hm.put("hash","test");

ok

final Map hm= new HashMap();
hm.put("hash","test");// this should not allow here as Map i have declared as final.

ko

but Map hms = Collections.unmodifiable(hm) is working fine.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Prabhat Ranjan wrote:final Map hm= new HashMap();
hm.put("hash","test");// this should not allow here as Map i have declared as final.

But hm is still pointing at the same object. final means that the reference can't be altered, not the object itself.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


hm.put("hash","test");// this should not allow here as Map i have declared as final. [

Final does not imply unmodifiable, unless the object is immutable. If you want an unmodifiable map have a look at the Collections class, or alternatively wrap the map in your own custom immutable class.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
you mean to say like String is final class.

only that type of classes are final in nature.

if same way
in Abstract Class

interface testFinal
{

Map hm = new HashMap();
}

in the above case it is implictily final
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

I think you're confusing several different concepts here.

A final class is a class that cannot be subclassed. There's nothing to prevent you having a final class that is modifiable, it simply means that you can't inherit from it.

An immutable, or unmodifiable, class is a class where the state cannot be changed because there are no methods available to change the state. Assuming you've made all the member variables private then objects can only be changed via setter methods etc...and if you haven't provided any then it's immutable.

(String happens to be an example that is both final and immutable).

A final reference is a variable that, once initialised, cannot be changed. A reference being final is completely unrelated to whether the object it's pointing to is immutable or an instance of a final class.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
i am sorry you are correct.

Actually final applied for all class,variable and method.

Got the point.
Means applying final in Map . like Final Map doesn't make sence and doesn't do any thing.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
And What about the Enumeration and Iterator() ;

As enumeration doesn't allow the modification to Collection
while Iterator() allows Modification.

And that is applied to only COllection framework while Map is not a collection.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


like Final Map doesn't make sence and doesn't do any thing.

Well, to be fair it does do something, just not the something you want. It prevents this:



And What about the Enumeration and Iterator() ;

As enumeration doesn't allow the modification to Collection
while Iterator() allows Modification.

And that is applied to only COllection framework while Map is not a collection.

What about them? Not sure I understand your question.
 
Don't get me started about those stupid light bulbs.
 
subject: Why final is not applied for Map ?