Forging The Rings!
From Huben's Wiki
(→Forging A Ring) |
|||
Line 30: | Line 30: | ||
Setting the metal and givenTo fields with the values of the arguments is obvious. The metal field will never be able to be set again for this instance, because it is final. 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. The metal field will never be able to be set again for this instance, because it is final. 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! | ||
− | Add a toString() method to write: "I am a X ring, made for Y." X should be the metal field, Y should be the givenTo field. | + | * Add a toString() method to write: "I am a X ring, made for Y." X should be the metal field, Y should be the givenTo field. |
− | Add a static forged() method that returns the String "There have been N rings forged." N should be numberForged. | + | * Add a static forged() method that returns the String "There have been N rings forged." N should be numberForged. |
Let us now show how this is used to forge rings! | Let us now show how this is used to forge rings! |