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 that will be treated as one instruction line. You will see this used often in Chapter 6, "Control Flow," where control constructs such as the if and while statements are introduced. These control directives determine whether, and how many times, a statement is executed, but the associated statement is most often a block.

There is nothing in the Java specification that prevents you from breaking your code into blocks even when they are not specifically called for, but this is seldom done. Unless separating out a segment of code adds to its readability as a unit, you should use blocks only when they are required to achieve correct execution of your code.

Tip

Although there is no limit to the number of statements included in a block, you should break your code down into relatively short methods that are each focused on a specific task. A long body typically results in a method that is difficult, if not impossible , to reuse because it is performing a sequence of tasks needed by a specific application instead of splitting the work into more manageable pieces. There is no strict guideline to follow, but if you define a method that cannot be printed on a single piece of paper, you should con sider breaking some of the functionality out into smaller supporting methods.


Labeled Statements

You can assign a label to a statement as a way to reference it from other parts of a method. Any statement in Java can have a label, which consists of an identifier followed by a colon . The identifier part is like any other identifier in that its name must only contain the allowed characters , it cannot be the same as any reserved word, and it cannot be the same as any already declared local identifier. If a label has the same name as a variable, method, or type name that is available to the block that contains it, the label takes precedence within that block and hides the outside variable, method, or type. A labeled statement looks like the following:

 myLabel: System.out.println("This is a labeled statement"); 

Labels are used only by the break and continue statements that are discussed in Chapter 6.

Scope

Another use of blocks is to control what is known as the scope of a variable. When you declare a variable within a method, it is only available for use within the code block that contains the declaration. For instance, consider the following code fragment:

 {    int x = 5; } System.out.println("x is =" + x); // This line is not valid. 

The last line of this code is not valid. The variable x is created for use within the block that declares it and discarded when the end of the block is reached. The variable is no longer within scope when the println is attempted.

Local Variable Initialization

Some programming languages automatically assign an initial value, such as zero, to a variable when it is declared but not explicitly initialized . Unfortunately, some languages will also allow you to use an uninitialized variable without assigning any consistent default value to it. In this case, you might unintentionally access a garbage value attached to a variable.

In Java, local variables declared within a method are not assigned an initial value, but they cannot be used before an explicit assignment is made. As a habit, you should normally go ahead and assign an initial value to a local variable as part of its declaration. If you don't do this, and the compiler sees any possibility of a variable being referenced prior to an assignment, it will report an error.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net