In this article, we will discuss about the java.lang.StackOverflowError
by looking different code examples and also how can we avoid them.
Table of Contents
More precisely, StackOverflowError
is an error which Java doesn’t allow to catch, for instance ,stack running out of space, as it’s one of the most common runtime errors one can encounter, because it’s raising condition is implemented directly or indirectly in almost every module.
The main cause of the java.lang.StackOverflowError is that we haven’t provided the proper terminating condition to our recursive function or template, which means it will turn into an equivalent of an infinite loop.
How JVM invokes a method
To understand about java.lang.StackOverflowError
, we need to understand how JVM invokes method internally. When a method is invoked, a new stack frame is created on the Thread Stack Size or on the call stack.
Now, that respective stack frame holds arguments of the invoked method, mainly the local variables and the return address of the that method. This creation of stack frames will be an iterative process, and will be stopped only when the end of method invocations is found inside nested methods.
In amid of this whole process, if JVM is running out of space for the new stack frames to be create, it will throw a java.lang.StackOverflowError
.
( This is the reason, why we call it as JVM error also. )
Reasons for java.lang.StackOverflowError
There can be multiples reasons for java.lang.StackOverflowError
Poor or No Termination Condition
This is most common situation termed as unterminated or infinite recursion.
Let’s see an example where the error will be thrown as a result of the given condition:
In the following example, we are trying to print natural numbers, without providing the proper condition.
< pre class= "java" name= "code" >
public class java2blog
{
static int i=0 ; // declaring static variable it cannot be from a static context
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static int printNum (int x) // declaring static because //non-static method printNum(int) cannot be referenced from a static context { i=i+1; System.out.println(i); return i+ printNum(i+1) ; } public static void main (String[] args) { java2blog.printNum(i) ; } |
}

Output:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
..
..
Exception in thread "main" java.lang.StackOverflowError
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
1 2 3 |
Now, we can see due the absence of the termination condition the error was thrown. Also, note that these repeating line numbers will be infinite and they indicate that this function is being called recursively. |
Solution
1 2 3 4 |
• After you notice these repeating pattern, try to introduce a terminating condition or some base condition for the recursive calls. • Another approach could be, if you notice that you have implemented correctly still the same error is encountered, then we can avoid that by increasing the stack’s size in order to allow the larger number of recursive calls. |
This can be easily done by changing the settings of our compiler.
< pre class= "java" name= "code" >
public class java2blog
{
static int i=0 ; // declaring static variable it cannot be from a static context
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public static int printNum (int x) // declaring static because //non-static method printNum(int) cannot be referenced from a static context { i=i+1; System.out.println(i); if(i==20) // Terminating condition introduced return i; return i+ printNum(i+1) ; } public static void main (String[] args) { java2blog.printNum(i) ; } |
}
Output: 

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Due to the Cyclic Relationships Between Classes
Cyclic Relationship : When two different classes instantiate each other inside their constructors causes this relationship.
This ends up with a StackOverflowError because the constructor of ClassA is instantiating ClassB, and the constructor of ClassB is again instantiating ClassA, and it occurs repeatedly until the overflow.
< pre class= "java" name= "code" >
public class A
{
public B type2 ;
public A() {
type2 = new B () ; // object of class B is created and hence constructor of // B is invoked
}
1 2 3 4 |
public static void main ( String[] args) { A type1 = new A () ; // starting the cycle by invoking class A constructor |
// at the time of object declaration (implicit)
}
}
class B
{
public A type1 ;
public B () {
type1= new A () ; // object of class A is created and hence constructor of // A is invoked
}
}
Output:
Exception in thread “main” java.lang.StackOverflowError
at B.
at A.
at B.
at A.
at B.
at A.
at B.
at A.
at B.
at A.
at B.
at A.
at B.
Repetitive calls here shows the infinite recursion
Solution
Now, the error here is mainly due to the constructors calling each other repetitively, so we have to make sure that while implementing different classes, the above condition should not arise.
Since there is no significance of this cyclic constructors invocations so we can avoid them, as they are already invoked implicitly at the time of the creation of the objects of their respective classes.
That’s all about java.lang.StackOverflowError in java.