I've got to explain this to my ten year old son, so please explain this in an easy way.
(If you can, each and every thing about this() .)

Thanks in advance.

Recommended Answers

All 7 Replies

Here is the file:-

it's explained pretty clear in the book:

public MyClass{

  public MyClass(int number){
    this("guess what number is printed: ", number);
  }

  public MyClass(String text, int number){
    System.out.println(text + number);
  }

  public static void main(String args[]){
    MyClass c = new MyClass(5);
  }
}

the two constructors are overloading each other (same method/constructor, different signature = other parameters)
by using the this(String, int)
you are merely calling the overloading constructor that take a String and an int as parameter

Ah, overloaded constructors. A question from my side, how much does your child know about overloading?

I'll try to explain:
We have a class, Box, and we want to have three constructors for it, one for each possible way to make class. The first possibility is a box of which we don't know any sizes, the second possibility is a box where we know that all sizes of the sides are equal (a cube!), and the third possibility is a box where we know all the sides, and they all have a different size.
Now we make the class and the three constructors for our class:

class Box {
Box () {

}
Box (int a) {

}
Box (int a, int b, int c) [

}
}

When we have our class, we are lazy. Because we don't want to type our initializing code three times, we call our last constructor from the first two constructors. We will just pass along some extra values. For the first box, we assume all the sizes are 1, because no information is given. For the second constructor, we can call the third using the given size for each side:

class Box {
Box () {
System.out.println ("I'm a box, with unknown sides.");
this(1,1,1); // equal to calling this(1), although this is directer
}
Box (int a) {
System.out.println ("I'm a box, with all sides "+a+".");
this (a,a,a);
}
Box (int a, int b, int c) [
System.out.println ("I'm a box, with sides "+a+", "+b+" and "+c+".");
}
}

What does the this() do here? We call another constructor of the same class, but with a different number (or a different kind) of parameters. This way we can redirect a call to a constructor to another constructor.

(Exercise, swap this(1,1,1) for this(1), and note the change in output.)

Ask me if you have any more questions.

But how does this() know WHICH overloaded constructor to call?

By matching the number and types of the parameters in the call to those in the constructor's definition (just like any overloaded method call)

commented: beaten by the punch :) +14

just the same way as any other method, by the parameters you pass
if you say:

int a = 1;
int b = 2;
this(a,b);

he will be calling the constructor which takes two ints as parameters.

Thanks, again, all of you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.