wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes final keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final keyword" Watch "final keyword" New topic
Author

final keyword

aakash bhat
Greenhorn

Joined: Jun 22, 2005
Posts: 2
WHat is the differnce between
HashMap vHashMap = new HashMap();
and
final HashMap vHashMap = new HashMap();

Thanks,
aakash
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

The only difference is that in the second version, the variable can't later be changed to refer to another, different HashMap object. It will always refer to the same object.


[Jess in Action][AskingGoodQuestions]
aakash bhat
Greenhorn

Joined: Jun 22, 2005
Posts: 2
If i later object would not be changed then is it better always to use final keyword. Do i get a performance benefit.?
Thanks,
aakash
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
final is useful documentation to humans that you never plan to change something, and it allows the compiler to do some smallish optimizations. It's also necessary if you want to reference a local variable from an inner class, but that's probably not for the beginner forum. I don't see many people use it heavily. Maybe we should get more used to it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
I was literally about to post a similiar question. As I have to mark certain variables final for useage inside an inner class and wanted to know if there were any side effects of doing such that I should know.

Final more or less describes the variable saying this variable will always reference this object? but in the previously mentioned hashmap you can update the map adding k,v pairs still right?

But in the case of a primitive (say an int), your stuck with the value you put in it yes?

If you used an Integer object instead would you be able to change the value its holding? Something seems wierd about that.

Is the final requirment on variable names referenced in classes their for a particular reason? The only thing I can think is that new object needs to know where to reference the variable... and assigning new objects the variable moves its location in memory, or am I way offbase?

-Tad
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Well google is your friend...

http://renaud.waldura.com/doc/java/final-keyword.shtml

I've only skimmed the article but it seems to confirm what I was thinking and delve into a lot more about how/why to use the final keyword.

-Tad
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Your understanding about what "final" does is correct, except for your conjecture about Integer -- because Integer is immutable: there's no way to change the int value one represents, final variable or not.

The "final" is required for anonymous inner class access to maintain the illusion that the anonymous inner class is actually using the local variables. It's not -- the locals are copied into the anonymous object when it is constructed and stored in member variables added by the compiler. If the locals could be changed, then the illusion that the anonymous class was using the locals would then be shattered: you would see that any changes to the locals were not reflected in the anonymous class instance.

It's necessary to do this because the anonymous object may (and usually does) continue to live after the method creating it returns. The stack where the locals live is thus often gone before the anonymous object would even look for them -- so it needs its own copies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final keyword
 
Similar Threads
hashmap query?
HashMap Construction with initial values
Facing null pointer exception in static initialization
Why final is not applied for Map ?
code conventions and constants variables