+ 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( ); } }
4 Antworten
+ 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.
+ 14
You're welcome ^^
+ 1
thank you