This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Recursive constructor invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive constructor invocation" Watch "Recursive constructor invocation" New topic
Author

Recursive constructor invocation

Philip Pomario
Ranch Hand

Joined: Oct 03, 2003
Posts: 113
Anyone knows when Sun's J2SE SDK compiler javac started detecting recursive constructor invocation?

e.g.:
class Explode{
Explode(){
this("now!");
}
Explode(String when){
this();
}
}

Explode.java:5: recursive constructor invocation
Explode(String when){
^
1 error
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

JDK 1.4.1 .


[Jess in Action][AskingGoodQuestions]
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
is this always an error, or merely when the compiler can prove the recursion is endless?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I believe the compiler can detect if it is endless.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
this isn't recursive, it's circular


42
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by M Beck:
is this always an error, or merely when the compiler can prove the recursion is endless?


Since the this() and super() statements must be the first statement in a constructor, and since they therefore can't be controller by a conditional statement, I think any circular constructor invocation must be infinitely recursive.
Philip Pomario
Ranch Hand

Joined: Oct 03, 2003
Posts: 113
Thanks for all the help and new thoughts on the topic!
Philip Pomario
Ranch Hand

Joined: Oct 03, 2003
Posts: 113
I just passed the SCJP1.4 exam and wanted to post this appreciation note to thank everyone who helped me understand Java a little better. Without your help this personal achievement wouldn't be possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive constructor invocation