Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860
Forging The Rings! - Huben's Wiki

Forging The Rings!

From Huben's Wiki
Jump to: navigation, search
Line 63: Line 63:
 
* The oneRing instance is created by calling the constructor and setting the constructor argument theMetal to "gold" and whom to "Sauron".
 
* The oneRing instance is created by calling the constructor and setting the constructor argument theMetal to "gold" and whom to "Sauron".
 
* Now when we call Ring.forged(), it shows that 1 ring has been forged.
 
* Now when we call Ring.forged(), it shows that 1 ring has been forged.
* '''Ring[] elvenRing = new Ring[3]; ''' creates an array of three Ring ***VARIABLES***.  It does not create any Ring instances.
+
* '''Ring[] elvenRing = new Ring[3]; ''' creates an array of three Ring ***VARIABLES***.  It does not create any Ring instances. Note the peculiar use of the square brackets instead of parentheses: no constructor is being called!
 
* Ring.forged() shows that we have not created any new instances.
 
* Ring.forged() shows that we have not created any new instances.
* So what is in those three Ring variables, if they are not pointing to Ring instances?  The value '''null'''.  When we try to println one of the Ring variables, elvenRing[0], the result is a null.
+
* So what is in those three Ring variables, if they are not pointing to Ring instances?  The value '''null'''.  When we try to println() one of the Ring variables, elvenRing[0], the result is a null.
 
* We laboriously create three Ring instances and assign them to each Ring variable in the array.
 
* We laboriously create three Ring instances and assign them to each Ring variable in the array.
 +
* Now we can print out all the rings created, and how many were forged.
 +
* Notice that your toString() method is used explicitly to print elvenRing[1].  Then println() prints the string value that is returned.  But you obviously don't need to do that: we don't call toString() elsewhere.  What's going on?  Well, println() asks every object it is passed to use its toString() method, and then prints the string.  If you don't write your own toString(), there is a default toString() that prints the object's class and location.

Revision as of 14:07, 18 October 2012

Personal tools
translate