File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes why 'should' one declare passed exception constructor parameters as final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why Watch "why New topic

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

Stu Thompson
Ranch Hand

Joined: Jun 14, 2006
Posts: 136
I've recently integrated checkstyle ( 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.

[ August 29, 2006: Message edited by: Stu Thompson ]

"This is not to say that design is unnecessary. But after a certain point, design is just speculation." --Philip Chu
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

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
I agree. Here's the link:
subject: why 'should' one declare passed exception constructor parameters as final?
jQuery in Action, 3rd edition