Why instantiating objects of same class within its constructor throws StackOverflowError?
For instance ,
public class A {
public A () {
A a = new A()
}
}
will throw StackOverFlowError ?
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.
It’s exactly the same as with any other function unconditionally calling itself with exactly the same parameters:
The function just keeps calling itself, and each invocation requires stack space. Sooner or later the stack is exhausted, and you get an exception.
Exactly the same thing happens when
A()keeps calling itself (innew A()).