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