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.
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.
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: 837
posted
0
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.
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.
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: 837
posted
0
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)
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: 837
posted
0
Thanks Rob. Your responses are always crystal clear to me.
Max Rahder
Ranch Hand
Joined: Nov 06, 2000
Posts: 177
posted
0
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: 837
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: A method argument that is never assigned can be declared final ?