We just started learning about class constructors in my Java programming class. In the past, we just did the new operator without knowing what was going on. Now we are getting a little more information on the subject. There is one specific constructor issue of interest to me. That is the case where a constructor calls another constructor. My book instructed me to use the “this” keyword to accomplish it as so:public Class Pet
{
public Pet()
{
// default constructor code
}
public Pet(String name)}
{
// call the default constructor
this();
}
I found this syntax to be a little strange. Yes I understand the “this” keyword refers to the current object. But we are talking about making a call to a method in the class. Why not use the name of the method like so:
public Class Pet
{
public Pet()
{
// default constructor code
}
public Pet(String name)}
{
Pet(); // This does not work!
}
It seems logical to use the actual method name when making a call to it. However the Java designers must not have agreed with me. Calling the constructor by its name results in a compile time error. Oh well. It was worth a try.
