Calling a constructor from another constructor is quite natural, though to you it may currently seem like a complicated acrobatic feat. Carefully study the program, understand what it does, and fix the constructor with two parameters so that it calls another constructor with a radius of 10. Which one? Guess!)
Calling a constructor from a constructor
- 4
Locked
Comments (17)
- Popular
- New
- Old
You must be signed in to leave a comment
Lance Wilson
22 November 2021, 04:47
would have been nice if "this" was explained in the lessons. no pun intended. it makes sense, after reading the descriptions/examples posted in the comments below.
0
Ice_ Beam
25 May 2021, 19:51
Would this technically work as well? (Just for clarification)
this.x = x;
this.y = y;
this.radius = 10
is the constructor chaining just the compact version of the regular this. assigning?
+4
Joe M
17 October 2020, 17:38
This is helpful, didnt know you could call a constructor from a constructor, makes sense
0
Karas Java Developer
30 August 2020, 23:09
This is technically called constructor chaining. It is actually very usable and makes programs easier to read actually.
+2
Martzehh
1 June 2020, 18:41
Solved the question, but like others have said below me I'm not entirely sure how or why I would implement this in my own program. Wish the lesson would elaborate more on concepts like this.
Maybe it's just to showcase the flexibility of the language?
+3
BlueJavaBanana
8 April 2020, 11:03
Could someone clarify the benefits of this? Does it offer any advantage OR is it simply another way of making constructors that ensure all variables get assigned a value?
+2
Ted404
29 February 2020, 22:08
I didn't understand what really happened here. Can anyone explain me please?
+2
AmberWings
3 October 2021, 10:22
I have got good explanation, just copying it here:
Using this with a Constructor
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section.
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor creates a 1x1 Rectangle at coordinates 0,0. The two-argument constructor calls the four-argument constructor, passing in the width and height but always using the 0,0 coordinates. As before, the compiler determines which constructor to call, based on the number and the type of arguments.
If present, the invocation of another constructor must be the first line in the constructor.
0
Raj Mishra Java Developer at Sopra banking softwa
1 October 2019, 13:51
I dont understand why my 4th validation is failing. Can any one pls suggest?
0
Roman
2 October 2019, 06:01
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
0
Gábor
31 July 2019, 17:02
If somebody can explain what was it, I would be thankfull...
No idea about this...
0
Roman
1 August 2019, 06:18
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
0
Fadi AlSaidi
19 November 2018, 08:06
I don't get it! I had to google it and it worked, but I don't recall going over this topic at all. So, I am trying to wrap my head around the concept. If I use the phrase"this" it would be a substitute to using the constructor's name? maybe someone from a higher level or experience can shed some light on this :)
+1
Justin Smith
7 October 2019, 03:05
I think their goal here isn't to give you all of the answers. At first glance, it doesn't make sense to teach in this method. However, in this field, you will be looking for answers. A lot. Emphasis on that last sentence. They are attempting to teach you to look outside this site, outside your direct instruction to find what you need.
As far as the exercise goes, you can chain to only one constructor. The first one you create at the top will be the "this". Since you pass arguments to the constructor, theres no need to write out "this.variable = variable" because the arguments are applied directly to the other constructor, where those assignments are already defined.
+4