Java Reference
In-Depth Information
final
The
final
modifier specifies that the class may not be extended. A class cannot
be declared to be both
abstract
and
final
.
strictfp
If a class is declared
strictfp
, all its methods behave as if they were declared
strictfp
. This modifier is extremely rarely used.
Fields and Methods
A class can be viewed as a collection of data (also referred to as
state
) and code to
operate on that state. The data is stored in fields, and the code is organized into
methods.
This section covers fields and methods, the two most important kinds of class mem‐
bers. Fields and methods come in two distinct types: class members (also known as
static members) are associated with the class itself, while instance members are
associated with individual instances of the class (i.e., with objects). This gives us
four kinds of members:
• Class fields
• Class methods
• Instance fields
• Instance methods
The simple class definition for the class
Circle
, shown in
Example 3-1
, contains all
four types of members.
Example 3-1. A simple class and its members
public
class
Circle
{
// A class field
public
static
final
double
PI
=
3.14159
;
// A useful constant
// A class method: just compute a value based on the arguments
public
static
double
radiansToDegrees
(
double
radians
)
{
return
radians
*
180
/
PI
;
}
// An instance field
public
double
r
;
// The radius of the circle
// Two instance methods: they operate on the instance fields of an object
public
double
area
()
{
// Compute the area of the circle
return
PI
*
r
*
r
;
}