This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why 'should' one declare passed exception constructor parameters as final?

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've recently integrated checkstyle (http://checkstyle.sourceforge.net/) into my build. It reports on a defined (with XML) coding convention. Thinking it would be good to follow the official Sun Java Coding Convention, my build is now configured with the included Sun convention adherence report.

One of the deviations from convention it reports is with all of my exception constructors is "Parameter x should be final."

So this leads me to the question, why 'should' one declare passed exception parameters as final? I've never seen this before. I can sorta see the logic but am wondering if this is overkill or if maybe there is some other deep thought I am missing.

Thanks for your input.

Stu
[ August 29, 2006: Message edited by: Stu Thompson ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can treat a parameter like a local variable, modify it and play with it all you want. This is considered A Bad Thing, though I would not consider it a hanging offense. Your syntax checker is warning you that it would be better to use a local variable for any modification, and using final will force you to do that.

Here's something I might have done in a weak, lazy moment when somebody sent me a badly formed path:

That would be better as

I'm pretty sure I know of at least one rancher who always uses final on parameters, but not very many. As good and proper as it is, it doesn't add to readability and the worst thing that can possibly happen when we leave it out is not all that bad.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic