Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
VS Prapoorna
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic