Blocks and Statements

The method body is where the work of a method is performed. This body is defined by a block of statements, which is a series of statements enclosed within curly braces { } . You can substitute a block of statements anywhere you can use a single statement.

The simplest block, { }, is shown in the following example:

public void emptyMethod() {
}

The next example is only slightly more complex:

public void simpleMethod(){
    int test;
    test = 5;
}

Code blocks are essential for defining the body of a method, but they can also be used in a variety of locations throughout your code. One very important aspect of a block is that it is treated lexically as one instruction. This means that you can put together large blocks of code ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.