Forging The Rings!
From Huben's Wiki
(→Forging A Ring) |
(→Another Constructor) |
||
Line 70: | Line 70: | ||
* 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. | * 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. | ||
== Another Constructor == | == Another Constructor == | ||
− | Classes can have more than one constructor. We are going to add a simpler constructor to class Ring, called a '''no-arguments constructor'''. It will provide default values for the two instance fields and update the static field. Here is the direct way to do it: | + | Classes can have more than one constructor. This is called '''overloading'''. We are going to add a simpler constructor to class Ring, called a '''no-arguments constructor'''. It will provide default values for the two instance fields and update the static field. Here is the direct way to do it: |
<pre> | <pre> | ||
public Ring() | public Ring() |