What does this(param); mean?

K

King W.Wang

Hi all,
in a program there is the line:

this(param);

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

king
 
N

nils

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top