This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor are playing with each other ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor are playing with each other ..." Watch "constructor are playing with each other ..." New topic

constructor are playing with each other ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

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

Joined: Feb 27, 2002
Posts: 8927

The compiler can detect these kind of recrusive calls. So I would select Compile error.

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 20, 2004
Posts: 82
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)?
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
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.
Have you checked out Aspose?
subject: constructor are playing with each other ...
It's not a secret anymore!