In: Computer Science
How is this method coded in Java?
First of all, let's see what the this() method
is:
this(); is a constructor which is used to call another constructor
in a class.
this() can have many uses. Some of them are as follows:
Please refer to the comments of the program for more clarity and to understand how this method is coded.
class Solution{
public Solution(int,int)
{ this(10, 20); // Here this will call default constructor
// Logic goes here
}
public Solution()
{
//code
}
public Solution(float,float)
{ this(); // Here this will call default type constructor
// Logic goes here
}
}
So, as we can see this() method is nothing but a method which is
ready-made for us to use. Implementation wise it is as similar to a
normal method.
Note: These things can be a little intimidating in
the beginning. But analysing it, again and again, will make it
simpler.
Please let me know in the comments in case of any confusion. Also, please upvote if you like.