What is the possible benefit (if any) of allowing recursive contructors?

Posted by Penang on Stack Overflow See other posts from Stack Overflow or by Penang
Published on 2010-04-07T18:32:19Z Indexed on 2010/04/07 18:43 UTC
Read the original article Hit count: 232

In Java, constructors cannot be recursive. Compile time error: "recursive constructor invocation". Let's assume that we did not have this restriction.

Things to keep in mind:

  • The return type of a constructor is void. Since it is a void method you can't harness the complete power of recursion.
  • A constructor can invoke itself (or any other constructor) using this(). But a "call to this must be first statement in constructor"
  • We could use non local data between consecutive calls to still have some possible gain from recursive constructors.

Would there be any benefit from allowing recursive constructors?

© Stack Overflow or respective owner

Related posts about recursion

Related posts about constructor