+ 7

Why StackOverflow Error ? [Solved]

This is simple java program. In this program i just created two objects and nothing else. Anyone know why this error is comming. https://code.sololearn.com/c14nDb01b6M2/?ref=app

2nd Sep 2020, 2:22 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
24 Respuestas
+ 5
P Jain you meant final keyword creating issue as per your last statement
3rd Sep 2020, 10:26 AM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 5
P Jain 😂 ok update
3rd Sep 2020, 10:30 AM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 4
You are making a new object of a class that's constructor also makes a new object of itself and that further makes new object and so on! Again again and again! So we are making unlimited number of objects of this class! You can add a static variable that is increased when an object is made (i.e. constructor) and put an if statement (or a base case) to prevent this Error!
2nd Sep 2020, 2:34 PM
Namit Jain
Namit Jain - avatar
+ 4
Namit Jain What do you want to explain in this code.
2nd Sep 2020, 2:52 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 4
Namit Jain we can put in block. If you heard about static and instance block.😅 And I'm not going to perform explicitly recursion
2nd Sep 2020, 3:15 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 3
P∆WAN M∆URY∆ Before calling constructor it call x1=new Program() ; This it self calls same class infinitely...
2nd Sep 2020, 2:37 PM
Jayakrishna 🇮🇳
+ 3
Namit Jain which line creating issue?
2nd Sep 2020, 2:45 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 3
Namit Jain if I will comment line 10 will it give me error ?
2nd Sep 2020, 2:48 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
2nd Sep 2020, 2:52 PM
Namit Jain
Namit Jain - avatar
+ 3
Namit Jain In this program you are explicitly creating a recursion and break it. In my question I didn't use this.
2nd Sep 2020, 3:05 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 3
P∆WAN M∆URY∆ You cannot put an if statement outside a method 😏🤔 So, how will you stop it without if statement 😑
2nd Sep 2020, 3:11 PM
Namit Jain
Namit Jain - avatar
+ 3
P∆WAN M∆URY∆ yaa! Block is necessary 🙃 But i don't think we can declare variables here
2nd Sep 2020, 3:19 PM
Namit Jain
Namit Jain - avatar
+ 3
The most simple explanation here(For those who not understand stack and heap) : Basically you're initialising an object of the same class as a member variable. This causes the JVM to find the constructor of the Program which has not been invoked upon yet(because the JVM always inits the members first) so the constructor which is been called makes another call to the constructor and which again makes the same thing... Goes on... Results into Stack Overflow. Basically you can't create a final object of the same class as a member variable(because you'll have to initialize it either in class block or constructor block which'll cause Stack Overflow). you also can't initialize a normal member(in class or constructor block).
3rd Sep 2020, 10:18 AM
P Jain
P Jain - avatar
+ 3
P∆WAN M∆URY∆ no I meant that object as a member variable initialisation is the cause of the error. Oops looked up now ... 😂😂😂 By mistake final was typed... Wait I'll correct that.
3rd Sep 2020, 10:28 AM
P Jain
P Jain - avatar
+ 3
P∆WAN M∆URY∆ corrected 👍
3rd Sep 2020, 10:31 AM
P Jain
P Jain - avatar
+ 2
public class Program { // final Program x1 = new Program(); Program() { System.out.println("stack over?"); } public static void main(String[] args) { Program x1 = new Program(); } } P∆WAN M∆URY∆ Before calling constructor it call x1=new Program() ; This it self calls same class infinitely.....
2nd Sep 2020, 2:32 PM
Jayakrishna 🇮🇳
+ 2
P∆WAN M∆URY∆ that there should be a limit! Like here there is a limit of 50 The class should not make its instance if it exceeds the provided limit (50)
2nd Sep 2020, 2:54 PM
Namit Jain
Namit Jain - avatar
2nd Sep 2020, 2:48 PM
Namit Jain
Namit Jain - avatar
2nd Sep 2020, 2:51 PM
Namit Jain
Namit Jain - avatar