This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I was reviewing someone else's code recently and came across a method defined as below(Method names have been changed...)
Though the comments indicate the reason for having the two parameters, i really don't understand how they are being used. Is not the scope of the parameters limited to the someMethod() body? I haven't seen the anotherMethod() definition yet, but i believe it shouldn't matter. Can someone help me understand this? Thanks in advance
Primitives passed as parameters to a method will not be modified, but Objects will! Both you (the code block calling the method) and the method you pass an Object to contain a reference to the SAME Object. Try playing around with this simple program to gain a better understanding:
You should find that the primitive value doesn't change, but the Map's size does!
At first I thought you were asking about how they could make the parameters optional, and in case you don't know, you could just pass null arguments in place of the Object parameters (as long as that method was set up to handle null values when you didn't have anything to give it). [ October 27, 2004: Message edited by: Stephen Huey ]
Joined: Oct 09, 2003
Thanks for the reply. But i think i didn't make my question clear someMethod() has two parameters which are not being used within the method body. My original post has complete method definition as i found it. I just changed the method names. The method comments say the parameters are optional, sometimes they could be null. But within the method body since the parameters are not being used, are not they redundant? Can we not safely remove the paramters as shown below
I hope i was clearer this time. If not I will try to explain again. thanks
Everytime I've seen this type of situation it means that changing whatever calls this method is a pain. You're correct - since the method parameters are unused in the method they do not have to be there. However, if the method is called from many other places how difficult would it be to change those too? If it is easy then by all means do it.
<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Originally posted by Shreyas Reddy: The method comments say the parameters are optional, sometimes they could be null. But within the method body since the parameters are not being used, are not they redundant? Can we not safely remove the paramters[?]
It's possible that this class extends another class or implements an interface that defines that method signature, and this class is overriding/implementing it. If that's the case, removing the parameters will change the signature, most likely with detrimental results.
If that method only exists in this class, I'd wonder what the author was thinking and perhaps suggest a better tag for the parameters: "ignored".