Java Reference
In-Depth Information
public void setRate(double rate) {
this.rate = rate;
}
Notice that the setter method accepts only a single argument, as required by the
JavaBeans API . The accessor and mutator method generator creates these types
of methods from your field variables automatically, making the process quick and
painless. From the code-generation pop-up ( Code | Generate or Alt+Insert ) you
can select one of the getter/setter options:
Getter —Creates accessor methods for getting the current value of selected
fields
Setter —Creates mutator methods for setting the specified value to
selected fields
Getter and Setter —Creates both the get and set methods for selected fields
You can see an example of this feature in action in figure 3.19. For each field
selected, an appropriate set of getters and/or setters will be created and inserted
at the cursor.
If accessor and/or mutator methods already exist for any of the class's
fields, these fields don't appear in this dialog.
NOTE
3.7.3
Generating hashCode and equals methods
This feature helps you avoid one of the most common Java pitfalls: failing to
override the equals and hashCode methods together. According to the Java spec-
ifications, if two objects are equal according to the equals method, then calling
the hashCode method on each of the two objects must produce the same integer
result. Otherwise, bad things can happen, especially with regard to collections.
Figure 3.19
Another trivial yet common coding task is
the creation of accessors and mutators for
your class's fields. IDEA's code generator
lets you generate them all automatically
with one command.
 
 
 
 
Search WWH ::




Custom Search