Defining a method with an optional parameter

Discussion in 'Java' started by Ahmed Moustafa, Aug 24, 2003.

  1. Is there a way to define a method with an optional parameter? Or, is it
    must to define the method one time with the optional parameter and
    another without?

    Thanks in advance,
    Ahmed Moustafa, Aug 24, 2003
    #1
    1. Advertising

  2. Ahmed Moustafa

    Paul Tomblin Guest

    In a previous article, Ahmed Moustafa <> said:
    >Is there a way to define a method with an optional parameter? Or, is it
    >must to define the method one time with the optional parameter and
    >another without?


    I usually define it once without as a one liner that just calls the one
    with the parameter with a default value. I'll even do it in constructors
    like that:

    public DBScreenDay(Connection conn, int screen, Date date, long dayDuration)
    {
    this(conn, screen, date, dayDuration, -1);
    }


    --
    Paul Tomblin <>, not speaking for anybody
    As far as the laws of mathematics refer to reality, they are not
    certain, and as far as they are certain, they do not refer to reality.
    -- Albert Einstein
    Paul Tomblin, Aug 24, 2003
    #2
    1. Advertising

  3. Ahmed Moustafa

    Roedy Green Guest

    On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
    <> wrote or quoted :

    >Is there a way to define a method with an optional parameter? Or, is it
    >must to define the method one time with the optional parameter and
    >another without?


    Not in Java, but in other languages that produce byte code. You need
    to define two methods, one with the parameter and one without. The
    one without often is implement as a call to the one with, supplying a
    default value.

    The technique falls apart if you want many parms and many default
    values.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 24, 2003
    #3
  4. Ahmed Moustafa <> writes:

    > Is there a way to define a method with an optional parameter? Or, is
    > it must to define the method one time with the optional parameter and
    > another without?


    You use overloading to do this, e.g.

    public void foo(int x, int y) {
    // Do stuff
    }

    public void foo(int y) {
    foo(0, y); // Implicit x = 0
    }
    Tor Iver Wilhelmsen, Aug 25, 2003
    #4
  5. Ahmed Moustafa

    Dale King Guest

    "Ahmed Moustafa" <> wrote in message
    news:CG92b.1817$...
    > Roedy Green wrote:
    > > On Sun, 24 Aug 2003 20:09:56 GMT, Ahmed Moustafa
    > > <> wrote or quoted :
    > >
    > >
    > >>Is there a way to define a method with an optional parameter? Or, is it
    > >>must to define the method one time with the optional parameter and
    > >>another without?

    > >
    > >
    > > Not in Java, but in other languages that produce byte code. You need
    > > to define two methods, one with the parameter and one without. The
    > > one without often is implement as a call to the one with, supplying a
    > > default value.

    >
    > Is it coming in 1.5?


    Quoting Joshua Bloch from the article:
    http://developer.java.sun.com/developer/community/chat/JavaLive/2003/jl0729.
    html

    "I don't think we'll ever add support for default parameter values for
    constructors or methods. Overloading provides similar functionality, and we
    don't like having many ways of doing the same thing."

    --
    Dale King
    Dale King, Aug 25, 2003
    #5
  6. Ahmed Moustafa

    codemonkey

    Joined:
    Mar 2, 2010
    Messages:
    2
    Java ... syntax

    It's not exactly "optional parameters", but the Java does allow an arbitrary number of arguments as part of an array:

    See "Arbitrary Number of Arguments" on sun's web-site (I'd post the link, but the site won't let me)
    codemonkey, Mar 2, 2010
    #6
  7. Ahmed Moustafa

    grizzleybear

    Joined:
    Apr 17, 2012
    Messages:
    1
    i gues it is an old post, but I recently stumbled upon this area so wanted to share my idea:

    You can always use Java option parameter expression, for example:

    Code:
     
    Public Constructor (String arg1, Int arg2, String... arg3)
    
    You gotta remember that arg3 is expected to be an array. However, if you know that it is gonna be only one element i.e. arg3[0] you can simply do the following in your constructor definition body:
    Code:
    this.arg3 = arg3[0];
    
    With regards to your second question, YES you can do that too. But assuming that you want a seamless interface between client and server (sorry, I am too hooked into Client-Server model) you wanna be able to keep the number of constructors to minimum. It is not a MUST for your design, but certainly a good practice.

    Does this answer your question?
    grizzleybear, Apr 17, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    416
    Peter Koch Larsen
    Jul 2, 2004
  2. Otto Wyss
    Replies:
    6
    Views:
    431
    Jeff Flinn
    Nov 9, 2004
  3. Sam
    Replies:
    3
    Views:
    243
    Gerard Flanagan
    Mar 10, 2008
  4. Sinex

    Usage of Use="Optional" on a web method parameter...

    Sinex, May 1, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    155
    Sinex
    May 1, 2007
  5. Varun Goel
    Replies:
    1
    Views:
    126
    Axel Etzold
    May 14, 2008
Loading...

Share This Page