Java Reference
In-Depth Information
6. An exception parameter is created each time an exception is caught by a catch
clause of a try statement (§ 14.20 ).
The new variable is initialized with the actual object associated with the exception
11.3 , § 14.18 ). The exception parameter effectively ceases to exist when execu-
tion of the block associated with the catch clause is complete.
7. Local variables are declared by local variable declaration statements (§ 14.4 ) .
Whenever the flow of control enters a block (§ 14.2 ) or for statement (§ 14.14 ) , a
new variable is created for each local variable declared in a local variable declara-
tion statement immediately contained within that block or for statement.
A local variable declaration statement may contain an expression which initializes
the variable. The local variable with an initializing expression is not initialized,
however, until the local variable declaration statement that declares it is executed.
(The rules of definite assignment (§16) prevent the value of a local variable from
being used before it has been initialized or otherwise assigned a value.) The local
variable effectively ceases to exist when the execution of the block or for statement
is complete.
Were it not for one exceptional situation, a local variable could always be re-
garded as being created when its local variable declaration statement is ex-
ecuted. The exceptional situation involves the switch statement (§ 14.11 ) , where
it is possible for control to enter a block but bypass execution of a local vari-
able declaration statement. Because of the restrictions imposed by the rules of
definite assignment (§16), however, the local variable declared by such a by-
passed local variable declaration statement cannot be used before it has been
definitely assigned a value by an assignment expression (§ 15.26 ) .
Example 4.12.3-1. Different Kinds of Variables
Click here to view code image
class Point {
static int numPoints; // numPoints is a class variable
int x, y; // x and y are instance variables
int[] w = new int[10]; // w[0] is an array component
int setX(int x) { // x is a method parameter
int oldx = this.x; // oldx is a local variable
this.x = x;
return oldx;
}
}
Search WWH ::




Custom Search