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 25: Line 25:
 
}
 
}
 
</pre>
 
</pre>
Constructors such as this one are methods with arguments.  The arguments are variables. Their values are set when the constructor is called (right after a '''new''' keyword.)
+
Constructors such as this one are methods with arguments.  This constructor could be called with: '''Ring oneRing = new Ring("gold", "Sauron");'''
 +
The arguments are variables. Their values are set when the constructor is called (right after a '''new''' keyword.) theMetal is set to "gold" and whom is set to "Sauron".
  
 
Setting the metal and givenTo fields with the values of the arguments is obvious.  But the subtle thing is that the class variable numberForged, which started at zero, is now increased by one every time this constructor is called, which means for every ring instance created.  We are counting the number of rings created!
 
Setting the metal and givenTo fields with the values of the arguments is obvious.  But the subtle thing is that the class variable numberForged, which started at zero, is now increased by one every time this constructor is called, which means for every ring instance created.  We are counting the number of rings created!
 +
 +
Let us now show how this is used to forge rings!
 +
 +
== Building The Forge ==
 +
<pre>
 +
class Forge
 +
{
 +
  static public void main (String[] args)
 +
  {
 +
    Ring oneRing = new Ring("gold", "Sauron");
 +
    Ring[3] elvenRing;  // Array of 3 rings for elves.
 +
    elvenRing[0] = new Ring("silver", "elf");
 +
    elvenRing[1] = new Ring("silver", "elf");
 +
    elvenRing[2] = new Ring("silver", "elf");
 +
  }
 +
}
 +
</pre>

Revision as of 13:00, 18 October 2012

Personal tools
translate