Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (9.5k points)

Can anyone tell me what causes java.lang.StackOverflowError? 

1 Answer

0 votes
by (19.7k points)

Java.lang.StackOverflowError occurs when you do a recursive call to the method infinitely. This causes exhaustion of maximum size of the stack (where the method and fields are stored inside the method).  To stop it, you need to provide an if-else condition for the method to exit from the loop. 

Here’s an example code snippet:

public class Method_recursion  {  

static void my_method(){  

System.out.println("hello");  

my_method();  

}  

public static void main(String[] args) {  

my_method();  

}  

}  

output :

hello 

Hello

.

.

.

java.lang.StackOverflowError

 

Browse Categories

...