• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

not leading to stack overflow error

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thsi code is never ending process because here the method's are calling recursively each other
so it must lead to stack overflow error :roll:

and whi i run the program it stops after printing limited times.(not infinity times)
why it stops?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it depends on JVM. On my 1.3 it threw StackOverflowError, just as expected.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program will surely throw StackOverflowException, and it is indeed throwing it. Observe that, the statements in the constructors will NEVER be printed on the console, since the constructors are NEVER executed upto that point. The infinite recursion is in the initialization of instance members, and this initialization takes place BEFORE any constructor code is executed (after a call to "super()" of course). Thus, what you will see printed on the console is ONLY the call stack, after the exception has been thrown. Something like this :

Exception in thread "main" java.lang.StackOverflowError
at Y.<init>(YZ.java:2)
at Z.<init>(YZ.java:11)
at Y.<init>(YZ.java:2)
at Z.<init>(YZ.java:11)
at Y.<init>(YZ.java:2)
at Z.<init>(YZ.java:11)
at Y.<init>(YZ.java:2)
// and then there will be thousands of such alternate "at Z" and "at Y" lines.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of your program is:

at Z.<init>(Test.java:12)
at Y.<init>(Test.java:3)
at Z.<init>(Test.java:12)
...
Till a certain no. of times.

Now your program is not running normally and the StackOverflowError has been raised and what you see above is the stack trace done by your JVM. To confirm that the StackOverflowError is raised you can do the following:

public static void main(String s[])
{
System.out.println("Here it comes");
try
{
Z z=new Z();
}
catch(StackOverflowError e)
{
System.out.println("StackOverflowError encountered");
}
}
the output now prints - StackOverflowError encountered
 
Sidharth Panwar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! Nilesh already posted the answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic