Java Reference
In-Depth Information
You can use the length instance variable with these arrays as you would any other. The
following statement contains a three-dimensional array of integers and displays the num-
ber of elements in each dimension:
int[][][] century = new int[100][52][7];
System.out.println(“Elements in the first dimension: “ + century.length);
System.out.println(“Elements in the second dimension: “ + century[0].length);
System.out.println(“Elements in the third dimension: “ + century[0][0].length);
Block Statements
Statements in Java are grouped into blocks. The beginning and ending boundaries of a
block are noted with brace characters—an opening brace ( { ) for the beginning and a
closing brace ( } ) for the ending.
At this point, you have used blocks to hold the variables and methods in a class defini-
tion and define statements that belong in a method.
Blocks also are called block statements because an entire block can be used anywhere a
single statement could be used (they're called compound statements in C and other lan-
guages). Each statement inside the block is then executed from top to bottom.
You can put blocks inside other blocks, just as you do when you put a method inside a
class definition.
An important thing to note about using a block is that it creates a scope for the local vari-
ables created inside the block.
Scope is the part of a program in which a variable exists and can be used. If you try to
use a variable outside its scope, an error occurs.
In Java, the scope of a variable is the block in which it was created. When you can
declare and use local variables inside a block, those variables cease to exist after the
block is finished executing. For example, the following testBlock() method contains a
block:
void testBlock() {
int x = 10;
{ // start of block
int y = 40;
y = y + x;
} // end of block
}
Two variables are defined in this method: x and y . The scope of the y variable is the
block it's in, which is noted with the start of block and end of block comments.
 
Search WWH ::




Custom Search