Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursive constructor invocation

 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK 1.4.1 .
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this always an error, or merely when the compiler can prove the recursion is endless?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the compiler can detect if it is endless.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this isn't recursive, it's circular
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help and new thoughts on the topic!
 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic