aspose file tools*
The moose likes Java in General and the fly likes Is there somthing called optional parameters... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is there somthing called optional parameters..." Watch "Is there somthing called optional parameters..." New topic
Author

Is there somthing called optional parameters...

Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
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
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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 ]
Shreyas Reddy
Ranch Hand

Joined: Oct 09, 2003
Posts: 49
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
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
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.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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".
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
Perhaps the method signature has been designed with the likely future requirements?

On the face of it though it does seem redundant.

Regards,
Saket
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
This happens often.

Typically, it's because you are implementing some interface with an implementation that doesn't need the parameters to determine the required behaviour. Other implementations might.

The same can be said for checked exceptions declared to be thrown on an interface method, though implementations should leave the declaration off if it is not used as a matter of form.


Tony Morris
Java Q&A (FAQ, Trivia)
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Probably you can over load the method. You can write two methods one with parameters one without parameters.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there somthing called optional parameters...