File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is it possible to send a default value to a method in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to send a default value to a method in java?" Watch "Is it possible to send a default value to a method in java?" New topic
Author

Is it possible to send a default value to a method in java?

VS Prapoorna
Greenhorn

Joined: Oct 13, 2001
Posts: 22
hi,
i want to know wether we can send any default value to a method in java. actally my problem is to send a default value to a java method some times and some time depending upon some logic i want to give soem value to it. so plese help me out in this regard.
regards,
Sarada Prapoorna.V
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Hi there
You can't set up a method in Java to accept a varying number of arguments like you can in C++. What you can do is overload the method to accept different numbers of arguments. Something like this:

this is a fairly simplistic example but you should get the idea of how it works. In your code you would just call whichever method is most appropriate based on your logic.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
You can use overloaded methods like this.
To borrow the above example:

Here you supply default value 2 as the second int when the method is called with just one int parameter.
This way you have all the code in just 1 method.
VS Prapoorna
Greenhorn

Joined: Oct 13, 2001
Posts: 22
hi,
thanks alot for ur replies. actually i have the same anser in my mind but now i am sure of it. anyway thanks once again.
regards,
Sarada Prapoorna.V
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to send a default value to a method in java?