What does this(param); mean?

Discussion in 'Java' started by King W.Wang, Aug 22, 2003.

  1. King W.Wang

    King W.Wang Guest

    Hi all,
    in a program there is the line:

    this(param);

    does it invoke the constructor with the parameter param? Thanks.

    king
     
    King W.Wang, Aug 22, 2003
    #1
    1. Advertising

  2. King W.Wang

    Guest

    > In a program there is the line:
    >
    > this(param);
    >
    > does it invoke the constructor with the parameter param ?


    Yes, it does.

    This way a class can have multiple constructors without the need
    to duplicate code.

    In this example, the first two constructors (with one parameter)
    both call the constructor with two parameters.

    So the user can choose which constructor to use, but the constructor
    with the two parameters is always the one who does the job in the end :

    public MyClass(int number)
    {
    this(number, "empty");
    }

    public MyClass(String text)
    {
    this(0, text);
    }

    public MyClass(int number, String text)
    {
    myNumber = number;
    myText = text;
    }
     
    , Aug 22, 2003
    #2
    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. Geathaa
    Replies:
    2
    Views:
    703
    Geathaa
    Jul 30, 2003
  2. ectoplasm
    Replies:
    12
    Views:
    651
    Zorro
    Jul 28, 2005
  3. Eloff
    Replies:
    2
    Views:
    260
    Eloff
    Oct 12, 2008
  4. C Barrington-Leigh
    Replies:
    1
    Views:
    1,214
    Tim Leslie
    Sep 10, 2010
  5. Dave
    Replies:
    5
    Views:
    686
    John Bokma
    Apr 26, 2011
Loading...

Share This Page