• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is there a multiple execution of 2 instance methods when called once from the main method  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i have this following code.


this code when executed is giving me the following output.


i am calling 2 instance methods m1,m2 from the main method by the object "im". I have also declared a String array variable a and passed it in my main method which i called from within my main method itself .my general perception was that i would get the output of the 2 instance methods and not knowing what would be the output for calling the main method from within the main method. Can any one explain what exactly is happening here.

Thanks
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the output shows, your initial main() then calls itself again (main()), and that call calls itself, and so on...and on.

There is a limit to the size of the call stack (think of it as a breadcrumb trail so when Java exits a method it knows where it came from), and eventually your recursive calls to main() cause that to overflow.  In this case, when it is trying to do the println() call in m1().
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave, clearly understood why is it happening that way...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!