My questions is this:
Why can I not pass this to an explicit call to a constructor?
Example:
class MyClass {
MyClass x;
public MyClass(MyClass c) {
x = c;
}
public MyClass() {
this(this); // Error
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are trying to pass a reference to
thisfrom a constructor to another constructor on the same instance.In Java you cannot access
thisimplicitly or explicitly in the constructor before returning from an implicit or explicit call tothis()orsuper(). This is because the super class has not yet initialised.You may need to duplicate code in the constructor:
There may be ways to hack around it using a private constructor, but then you are into hacking territory.