r/learnjava • u/Eva_addict • 2h ago
I need help understanding the use of "this" inside a constructor.
*Sorry if I ask too much here but books seem to get more confuse the more you go though them*
I am learning using Deitel's book and in chapter 8.5 they have an example of Overloaded Constructors. They use them in class called Time2:
public Time2()
{
this(0, 0, 0);
}This exaplain it below but I can't understand it. In fact, I didn't even know this could be used like that. The book never showed it this way. Here it looks more like a method call. There are 5 constructors like that and each one adding one more argument to this and it makes everything else even more confuse.
3
Upvotes
•
u/AutoModerator 2h ago
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/desrtfx 1h ago
Constructor overloading - multiple constructors with different parameters - goes hand in hand with constructor chaining - what you are doing here.
One constructor
Time2()(no argument constructor, default constructor) calls anotherthis(0, 0, 0).The
thishere simply refers to a different constructor of the same class.In your particular case, it calls the constructor with 3 parameters, most likely
Time2(int hours, int minutes, int seconds)2
u/sozesghost 1h ago
It is a constructor call, which calls another, overloaded constructor from the same class, same instance even. You use it to have some base logic (assignement or validation) in one constructor, instead of duplicating it several times. The 'super' keyword can also refer to fields or methods or constructors, but from the parent class.
0
u/vegan_antitheist 2h ago
It works like a method call. You invoke the other constructor. Instead, you can call super(). Try a debugger to see how it works.