• 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

constructor are playing with each other ...

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


Although , it is clearly mention in K&B that , this type of error depends on type of compiler . But suppose this come in exam & options are :
1] compile time error .
2] run time error .

then what shuould be the answer .

thanks .
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler can detect these kind of recrusive calls. So I would select Compile error.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my compiler too ... but it is mention in K&B that most of the compiler will not be able to detect ...
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler detected recursive calls, too.
However, this was compiled and ran until reaching StackOverflowError:
Why are constructors a special case then?
Obviously, a method can call itself (to implement recursion). So, are two methods allowed to call each other recursively for the same reason?
Or is it just too much of a hustle for compiler to track recursive calls (especially in case of more than two methods)?
Thanks
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A recursive method call should have some terminating condition. If a programmer fails to provide that terminating condition, then that is his error and will result in a stack overflow.

A constructor, however, does not normally have a terminating condition, although I suppose you can simulate one by throwing an exception. Also, since a constructor does not have a retrun value, I don't know why a program would ever need to call a constructor recursively.
 
reply
    Bookmark Topic Watch Topic
  • New Topic