Revisit: What happens when a constructor references itself (by accident of course)?
Mar
2
Written by:
Monday, March 02, 2009 9:32 PM
Of course YOU'VE never done this before, where you write two constructors, but instead of having one calling the other, it calls itself.......hhhhmm what would happen? Read on for more details.
Have you ever written so many constructor's for your PERFECT class that one of them, uh, hmmm, calls itself? By accident of course, cause, uh, why the heck would you do it on purpose?! :>
As I was getting feedback about the last Coding Challenge: Can you spote the coding error, SG asked me what would happen if the constructor called itself? hhhmmm eventually it would run out of memory? Timeout? Overflow error? Sun would go bankrupt? Oops, sorry, wishful thinking on that last one! :> HAHA LOL! But seriously, do you really know exactly what would happen? Ok smarty pants (yup, being antagonistic again aren't I? :>) have you actually whipped up a test program to actually "experience" what would happen or are you just "supposing?"
Well, I whipped something up and here's what I got. Did you get the same?

That is one way to call itself, exactly 9602 times before getting the StackOverflowException. But I have another question for you, what is another way to get the same type of behaviour? Hint, it's in another blog entry I have about the chains of love! Hey, I can't give you all the answers! :>
Grab a coffee and get coding! :>
3 comment(s) so far...
Re: Revisit: What happens when a constructor references itself (by accident of course)?
What is with the Film theme of late? :P
By Sylvain on
Wednesday, March 04, 2009 10:13 AM
|
Re: Revisit: What happens when a constructor references itself (by accident of course)?
That's what the first example was written with. SB at work actually came up with the Film class example. I wonder maybe I should have did an example of Song with concrete instance called americanPie.
Song americanPie = new Song("American Pie", 1971, "http://en.wikipedia.org/wiki/American_Pie", "Song about The Day The Music Died.");
Would that work instead? :>
PS If you're following this and going WTF? Don't worry, inside and ongoing joke. :>>>>
By phenry on
Wednesday, March 04, 2009 10:20 AM
|
Re: Revisit: What happens when a constructor references itself (by accident of course)?
I so proved (SG) wrong on that one. :)
By Sylvain on
Wednesday, March 04, 2009 10:26 AM
|