Java Reference
In-Depth Information
The original version of the program consists of just one class,
Fruit
, and just one method,
main()
. Ex-
ecution of an application always starts at the first executable statement in the method
main()
. There are
no objects of the class
Fruit
defined, but the method
main()
can still be executed because I have spe-
cified it as
static
. The method
main()
is always specified as
public
and
static
and with the return
type
void
. The effects of these three keywords on the method are as follows:
public
Specifies that the method is accessible from outside the
Fruit
class.
static
Specifies that the method is a class method that is to be executable, even though no class objects
have been created. (Methods that are not static can be executed only for a particular object of the
class, as you will see in Chapter 5.)
void
Specifies that the method does not return a value.
Don't worry if these are not completely clear to you at this point — you see them all again later.
The first three statements in
main()
declare the variables
numOranges
,
numApples
, and
numFruit
to be
of type
int
and initialize them to the values 5, 10, and 0 respectively. The next statement adds the values
stored in
numOranges
and
numApples
, and stores the result, 15, in the variable
numFruit
. We then gen-
erate some output from the program.
Producing Output
The next two statements use the
println()
method, which displays text output. The statement looks a
bit complicated, but it breaks down quite simply, as
Figure 2-3
shows.