Programming and Problem Solving with Java

(やまだぃちぅ) #1
2.2 Application Construction | 73

The body of the method contains declarations of five Stringvariables (first,last,mid-
dle,firstLast, and lastFirst), a BufferedReadervariable (in), and a list of executable statements.
The compiler translates these executable statements into Bytecode instructions. During the
execution phase, these instructions are executed.
Note that neither the publicnor the privatemodifier precedes the fields declared within
main. Fields declared inside the body of a method are localto that method, which means that
they are accessible only within the method. Thus there is no need to explicitly specify that
they are private, and they cannot be made public. In fact, the only modifier allowed in a lo-
cal declaration is final.
Notice how we use spacing in the PrintNameapplication to make it easy for someone
else to read. We use blank lines to separate statements into related groups, and we indent
the entire body of the class and the mainmethod. The compiler doesn’t require us to format
the code this way; we do so only to make it more readable. We will have more to say in sub-
sequent chapters about formatting code.
Here is what the program displays on the screen when it executes:


Blocks


The body of a method is an example of a block. The syntax template for a block is as follows:


A block is just a sequence of zero or more statements enclosed (delimited) by a {}pair.
Now we can redefine a method declaration as a heading followed by a block:


Method-Declaration

Heading
Block

Statement...

Block
{

}
Free download pdf