File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final keyword

 
aakash bhat
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHat is the differnce between
HashMap vHashMap = new HashMap();
and
final HashMap vHashMap = new HashMap();

Thanks,
aakash
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
aakash bhat
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tad Dicks
Ranch Hand
Posts: 264
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic