File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes A method argument that is never assigned can be declared 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 "A method argument that is never assigned can be declared final ?" Watch "A method argument that is never assigned can be declared final ?" New topic
Author

A method argument that is never assigned can be declared final ?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I am running a PMD analyzer on my code. It has recommended me some good practices.

This is the recommendation given by it:

A method argument that is never assigned can be declared final.
Example:


Even the above recommendation is given by Eclipse 3.4. I enabled some code checks which are disabled by default.

But my code is:
I am assigning the method argument to the instance variable. So why it is offering me this recommendation.
Any Idea guys ?



SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Amandeep Singh wrote:But my code is:
I am assigning the method argument to the instance variable. So why it is offering me this recommendation.
Any Idea guys ?




Well, this is independent of what they are recommending. The method parameter dbRecordKey never finds itself on the LEFT side of an assignment. Which means you can make it final - which prevents accidental assignment and sometimes can help the Run Time run things more efficiently.

The fact that the parameter is on the RIGHT side of the equal sign has no affect. You can assign the value of a final variable to another variable if you want, no problems, it doesn't change the value of the variable in anyway.


Steve
Marc Maréchal
Greenhorn

Joined: Jan 08, 2005
Posts: 10
If your argument is not modified in the body of the function, declaring it final enables developper reading your code that they don't have to check if the argument is modified or not in the body of the function.


Marc Marechal
MCSE, CCNP, SCJP 5.0, SCBCD 5.0 (Beta), SCJD 6.0
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
As i am using the getter and setter methods. Making the method argument final is advised.

Will it not cause any other side effect. Because mostly i never see anywhere this type of advice
being implemented.

So here making the argument final means, nobody can change its value. Only it can be assigned to a new variable. Then modified.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Nope, no other side affects (as in unexpected behavior). Another affect it has, though, is you can directly use the method parameter inside anonymous inner classes you generate in the method. For example:


Because it has some benefits, and no real disadvantages, I tend to use it often.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
I agree with you. Then why it is not seen anywhere use of this practice. Why it is mostly ignored ?

Even in many books example, like making beans. This is being ignored. Do you know any API implementation where it is being used ?

So that i can follow that. Except the example given by you. Means API example similar to using the beans or my example.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Amandeep Singh wrote:I agree with you. Then why it is not seen anywhere use of this practice. Why it is mostly ignored ?

Because it requires 6 extra characters per parameter I have to type in myself

Honestly, the only reason I hardly ever use final parameters is laziness. I only make them final when I need it for inner classes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40


And plus, its for something that doesn't get you much.... setters are generally one line long. It only takes a split-second to notice that the param isn't changed in the method. And that is assuming that you even care. Laziness wins out here.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yeah-for setters there's hardly any point, except to make code quality tools be quiet.

For larger methods it can aid program correctness by avoiding unwanted assignments. I used to be a proponent of making method parameters final, but I eventually just turned the rule off because my methods are usually so short that it doesn't really aid in making my intent more clear, and I never assign to parameters anyway.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
All your replies give me the answer why it is being ignored.

Do any java API use this practice ? (if the answer is in top of your head)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Not many, as far as I've seen so far. Guess Sun's employees are just as lazy as we are
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Thanks Rob. Your responses are always crystal clear to me.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Amandeep Singh wrote:All your replies give me the answer why it is being ignored. Do any java API use this practice ? (if the answer is in top of your head)


For a while, WebSphere Application Developer (now called Rational Developer) used final parameters in generated setters. I'm not sure why they stopped -- maybe it caused confusion, since you don't see it very much.

I think it's a good practice, since it would avoid the mistake illustrated below:
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Max Rahder wrote:
Amandeep Singh wrote:All your replies give me the answer why it is being ignored. Do any java API use this practice ? (if the answer is in top of your head)


For a while, WebSphere Application Developer (now called Rational Developer) used final parameters in generated setters. I'm not sure why they stopped -- maybe it caused confusion, since you don't see it very much.

I think it's a good practice, since it would avoid the mistake illustrated below:



Max, i am confused with your reply. Which good practice you are referring to, whether to use the final or not as per my question ?
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Amandeep Singh wrote:Max, i am confused with your reply. Which good practice you are referring to, whether to use the final or not as per my question ?


The last one, with the parameter being declared as final.

I used to follow that practice, but I stopped, partly out of laziness, and partly out of not wanting to explain what I was doing over and over again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A method argument that is never assigned can be declared final ?