Forum Stats

  • 3,837,901 Users
  • 2,262,306 Discussions


Switch statement memory allocation question

ricardoprins Member Posts: 2
edited Mar 13, 2020 3:56AM in Java Programming


I am taking a Java class in school. We had this assignment to design a class to function as a menu, with several sub menus.

The structure is kinda like this (sort of pseudocode below, just to show structure):

static method mainMenu {


case 1: submenu1


case 2: submenu2



static method submenu1 {

case 1: subsubmenu1


case 2: subsubmenu2


default: mainMenu


static method subsubmenu1 {

case 1:  anothersubmenu1


case 2:  anothersubmenu2


default: submenu1


My question is: my teacher said this is wrong, because JVM stores in the memory the path that the program takes from one place to the other if I make it this way, and in the long run this would cause a stack overflow. He didn't quite explained it, he just said that I should surround the whole thing with a while loop using a boolean variable, adding an option to flip that boolean value to exit the while loop, because this way Java wouldn't be storing the path the program was taking from one method to the other.

Again, he didn't explain it with details, and it sounded very confusing the way he was explaining it (I tried to make it as clearly as I could, from what he has given me). I have been looking for the last 3 hours online for anything that resembled what he told me, and I couldn't find I decided to ask the experts.

Could you guys help me out?