Java Reference
In-Depth Information
• The primary purpose of
public
methods is to present to the class's clients a view of the services
the class provides. Clients need not be concerned with how the class accomplishes its tasks.
• A class's
private
variables and
private
methods (i.e., its implementation details) are not acces-
sible to its clients.
Section 8.4 Referring to the Current Object's Members with the
this
Reference
• An instance method of an object implicitly uses keyword
this
(p. 322) to refer to the object's
instance variables and other methods. Keyword
this
can also be used explicitly.
• The compiler produces a separate file with the
.class
extension for every compiled class.
• If a local variable has the same name as a class's field, the local variable shadows the field. You
can use the
this
reference in a method to refer to the shadowed field explicitly.
Section 8.5
Time
Class Case Study: Overloaded Constructors
• Overloaded constructors enable objects of a class to be initialized in different ways. The compiler
differentiates overloaded constructors (p. 324) by their signatures.
• To call one constructor of a class from another of the same class, you can use the
this
keyword
followed by parentheses containing the constructor arguments. If used, such a constructor call
must appear as the first statement in the constructor's body.
Section 8.6 Default and No-Argument Constructors
• If no constructors are provided in a class, the compiler creates a default constructor.
• If a class declares constructors, the compiler will not create a default constructor. In this case, you
must declare a no-argument constructor (p. 327) if default initialization is required.
Section 8.7 Notes on
Set
and
Get
Methods
•
Set
methods are commonly called mutator methods (p. 331) because they typically change a val-
ue.
Get
methods are commonly called accessor methods (p. 331) or query methods. A predicate
method (p. 332) tests whether a condition is true or false.
Section 8.8 Composition
• A class can have references to objects of other classes as members. This is called composition
(p. 332) and is sometimes referred to as a
has-a
relationship.
Section 8.9
enum
Types
• All
enum
types (p. 335) are reference types. An
enum
type is declared with an
enum
declaration,
which is a comma-separated list of
enum
constants. The declaration may optionally include other
components of traditional classes, such as constructors, fields and methods.
•
enum
constants are implicitly
final
, because they declare constants that should not be modified.
•
enum
constants are implicitly
static
.
• Any attempt to create an object of an
enum
type with operator
new
results in a compilation error.
•
enum
constants can be used anywhere constants can be used, such as in the
case
labels of
switch
statements and to control enhanced
for
statements.
•Each
enum
constant in an
enum
declaration is optionally followed by arguments which are passed
to the
enum
constructor.
•For every
enum
, the compiler generates a
static
method called
values
(p. 336) that returns an
array of the
enum
's constants in the order in which they were declared.
•
EnumSet
static
method
range
(p. 337) receives the first and last
enum
constants in a range and
returns an
EnumSet
that contains all the constants between these two constants, inclusive.