Executing main(...)

exercise No. 229

Q:

We consider:

Code
01 public class LackInit {
02 ...
03   static void main() {
04      main();
05   }
06 ...
Result
Exception in thread "main" java.lang.StackOverflowError
     at de.hdm_stuttgart.mi.sd1.extra.LackInit.main(LackInit.java:4)
     at de.hdm_stuttgart.mi.sd1.extra.LackInit.main(LackInit.java:4)
          ... 1020 more identical lines omitted for brevity ...
     at de.hdm_stuttgart.mi.sd1.extra.LackInit.main(LackInit.java:4)
     at de.hdm_stuttgart.mi.sd1.extra.LackInit.main(LackInit.java:4)

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

A:

The Javadoc of StackOverflowError already provides a hint:

Thrown when a stack overflow occurs because an application recurses too deeply.

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