Executing main(...)

exercise No. 204

Q:

Executing the following program

01 public class LackingInit {
02 ...
03   public static void main(String[] args) {
04      main(args);
05   }
06 ...

yields:

Exception in thread "main" java.lang.StackOverflowError
     at de.hdm_stuttgart.mi.sd1.extra.LackingInit.main(LackingInit.java:4)
     at de.hdm_stuttgart.mi.sd1.extra.LackingInit.main(LackingInit.java:4)
          ... 1020 more identical lines omitted for brevity ...
     at de.hdm_stuttgart.mi.sd1.extra.LackingInit.main(LackingInit.java:4)
     at de.hdm_stuttgart.mi.sd1.extra.LackingInit.main(LackingInit.java:4)

Process finished with exit code 1

What's happening here? Why is execution being terminated by an exception?

A:

Our main(...) method calls itself recursively until breaching the JVM's limit resulting in the observed StackOverflowError exception:

main(...) Stack frame 1
 ┗━▶ main(...) Stack frame 2
      ┗━▶ main(...) Stack frame 3
           ┗━▶ main(...) Stack frame 4
             ...
                ┗━▶ main(...) Stack frame 1024