Java Reference
In-Depth Information
Java syntax: Class definition
public class
class-name
{
declarations of methods and fields
Style Note
13.1, 13.1.3:
class name
conventions
}
Purpose
: To define a new file drawer, named
class-name
, and describe
the contents of its manila folders (instances, or objects, of the class).
Note that the class definition does not include the clause “
extends
…
”. This
class does not explicitly
extend
another; it stands alone, by itself. The subclasses
of
JFrame
that we wrote in Chap. 1 extended
JFrame
.
A class definition defines the contents of manila folders that are placed in its
file drawer. Such a manila folder is called an
instance of the class
, or an
object
of the class
. An instance of class
Employee
appears in Fig. 3.2, along with the
file drawer in which the instance appears. Each instance of a class contains the
following components:
Style Note
13.1, 13.1.1:
field name
conventions
1. The
fields
, or
instance variables
, of the class, which are the variables
defined in it that do not have modifier
static
.
2. The
instance methods
of the class, which are the methods defined in it
that do not have modifier
static
.
Initial values of fields
If a field is declared without an initializing assignment, the field has a
default value, which depends on its type. Here are the default values:
Activity
3-4.2
byte:
(
byte
)0
float
: 0.0F
short
: (
short
)0
double
: 0.0D
int
: 0
boolean
:
false
char
:
null character,
'\u0000'
long:
0L
class-type
:
null
a0
Employee
getName()
setName(String)
getStart()
setStart(
int
)
getCompensation()
changeSalary(
double
)
toString()
Employee(String,
int
)
name
Gries
start
1997
salary
50,000
Figure 3.2:
An instance of class
Employee
Search WWH ::
Custom Search