Search
Thursday, May 17, 2012 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

Revisit: What happens when a constructor references itself (by accident of course)?

Mar 2

Written by:
Monday, March 02, 2009 9:32 PM  RssIcon

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?

StackOverflowException for constructor calling itself

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! :>

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

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

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement