Java Reference
In-Depth Information
public static int getValue( int setNo, int i, int j) {
29
30
static method
return dates[setNo][i][j];
31 }
32 }
This class uses a three-dimensional array to store dates (lines 2-22). You could use a dif-
ferent data structure (i.e., five two-dimensional arrays for representing five sets of numbers).
The implementation of the getValue method would change, but the program that uses
GuessDate wouldn't need to change as long as the contract of the public method getValue
remains unchanged. This shows the benefit of data encapsulation.
The class defines a private no-arg constructor (line 25) to prevent the user from creating
objects for this class. Since all methods are static in this class, there is no need to create
objects from this class.
benefit of data encapsulation
private constructor
10.12 Why is the no-arg constructor in the Math class defined private?
Check
Point
10.11 Class Design Guidelines
Class design guidelines are helpful for designing sound classes.
Key
Point
You have learned how to design classes from the preceding two examples and from many
other examples in the preceding chapters. This section summarizes some of the guidelines.
10.11.1 Cohesion
A class should describe a single entity, and all the class operations should logically fit
together to support a coherent purpose. You can use a class for students, for example, but you
should not combine students and staff in the same class, because students and staff are differ-
ent entities.
A single entity with many responsibilities can be broken into several classes to separate the
responsibilities. The classes String , StringBuilder , and StringBuffer all deal with
strings, for example, but have different responsibilities. The String class deals with
immutable strings, the StringBuilder class is for creating mutable strings, and the
StringBuffer class is similar to StringBuilder except that StringBuffer contains
synchronized methods for updating strings.
coherent purpose
separating responsibilities
10.11.2 Consistency
Follow standard Java programming style and naming conventions. Choose informative names
for classes, data fields, and methods. A popular style is to place the data declaration before the
constructor and place constructors before methods.
Make the names consistent. It is not a good practice to choose different names for similar
operations. For example, the length() method returns the size of a String , a
StringBuilder , and a StringBuffer . It would be inconsistent if different names were
used for this method in these classes.
In general, you should consistently provide a public no-arg constructor for constructing a
default instance. If a class does not support a no-arg constructor, document the reason. If no con-
structors are defined explicitly, a public default no-arg constructor with an empty body is assumed.
If you want to prevent users from creating an object for a class, you can declare a private
constructor in the class, as is the case for the Math class and the GuessDate class.
naming conventions
naming consistency
no-arg constructor
10.11.3 Encapsulation
A class should use the private modifier to hide its data from direct access by clients. This
makes the class easy to maintain.
encapsulating data fields
 
 
Search WWH ::




Custom Search