• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor are playing with each other ...

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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 .
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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 ...
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic