Below mentioned piece of code gives output as "Stack overflow error". Could someone please explain the code? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Below mentioned piece of code gives output as "Stack overflow error". Could someone please explain the code?

class A { A a1=new A( ); public static void main(String [ ]args) { A a2=new A( ); } }

18th May 2017, 10:03 AM
Trideep Gayan
Trideep Gayan - avatar
3 ответов
+ 15
'Thrown when a stack overflow occurs because an application recurses too deeply.' From: https://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html In your example, the instantiation can't be completed, before class A is fully instantiated. To be completely instantiated, your class must create another instance of A (object a1). And that object must instantiate another object of A and so on and so on.
20th May 2017, 8:13 AM
Tashi N
Tashi N - avatar
+ 14
You're welcome ^^
20th May 2017, 7:46 PM
Tashi N
Tashi N - avatar
+ 1
thank you
20th May 2017, 7:35 PM
Trideep Gayan
Trideep Gayan - avatar