Java Reference
In-Depth Information
Provide a
get
method only if you want the field to be readable, and provide a
set
method
only if you want the field to be updateable. For example, the
Course
class provides a
get
method for
courseName
, but no
set
method, because the user is not allowed to change the
course name once it is created.
10.11.4 Clarity
Cohesion, consistency, and encapsulation are good guidelines for achieving design clarity.
Additionally, a class should have a clear contract that is easy to explain and easy to understand.
Users can incorporate classes in many different combinations, orders, and environments.
Therefore, you should design a class that imposes no restrictions on how or when the user can
use it, design the properties in a way that lets the user set them in any order and with any com-
bination of values, and design methods that function independently of their order of occur-
rence. For example, the
Loan
class contains the properties
loanAmount
,
numberOfYears
,
and
annualInterestRate
. The values of these properties can be set in any order.
Methods should be defined intuitively without causing confusion. For example, the
substring(int beginIndex, int endIndex)
method in the
String
class is some-
what confusing. The method returns a substring from
beginIndex
to
endIndex - 1
, rather
than to
endIndex
. It would be more intuitive to return a substring from
beginIndex
to
endIndex
.
You should not declare a data field that can be derived from other data fields. For example,
the following
Person
class has two data fields:
birthDate
and
age
. Since
age
can be
derived from
birthDate
,
age
should not be declared as a data field.
easy to explain
independent methods
intuitive meaning
independent properties
public class
Person {
private
java.util.Date birthDate;
private int
age;
...
}
10.11.5 Completeness
Classes are designed for use by many different customers. In order to be useful in a wide
range of applications, a class should provide a variety of ways for customization through
properties and methods. For example, the
String
class contains more than 40 methods that
are useful for a variety of applications.
10.11.6 Instance vs. Static
A variable or method that is dependent on a specific instance of the class must be an instance
variable or method. A variable that is shared by all the instances of a class should be declared
static. For example, the variable
numberOfObjects
in
CircleWithPrivateDataFields
in Listing 8.9 is shared by all the objects of the
CircleWithPrivateDataFields
class and
therefore is declared static. A method that is not dependent on a specific instance should
be defined as a static method. For instance, the
getNumberOfObjects
method in
CircleWithPrivateDataFields
is not tied to any specific instance and therefore is
defined as a static method.
Always reference static variables and methods from a class name (rather than a reference
variable) to improve readability and avoid errors.
Do not pass a parameter from a constructor to initialize a static data field. It is better to
use a
set
method to change the static data field. Thus, the following class in (a) is better
replaced by (b).