+ 1

why there is an error

class A{ int b = 6; int c = 7; A f = new A();// generate a error } why?

2nd Oct 2020, 5:07 AM
Zhengrong Yan
Zhengrong Yan - avatar
5 ответов
+ 2
David Yan You got StackOverflowError. Your A class has an instance of itself. When you create A class in your first example, another instance of A is created (A f = new A). This new instance f needs another instance of A, then another and so on, until you are out of memory. This error does not occur in the second example, because you do not initialize the inner object of A f. When you are creating instance of A in your main method, no other instances of A are created.
2nd Oct 2020, 6:25 AM
Aleksandrs
Aleksandrs - avatar
+ 4
its showing error defination of A is not complete may be 😆You creating object of class A inside of body . Why u need to create First read about class and object why we need of object .
2nd Oct 2020, 5:29 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Because A is not defined
2nd Oct 2020, 5:29 AM
Steve Sajeev
Steve Sajeev - avatar
+ 3
David Yan What error is thrown? Your code compiles. It is allowed to initialize objects inside body. https://code.sololearn.com/cmJi6i6y43TY/?ref=app
2nd Oct 2020, 5:43 AM
Aleksandrs
Aleksandrs - avatar