Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic