| variableNamesnutsandbolts variableInitialization |
The location of the variable declaration within your program establishes its scope and places it into one of these four categories:
You declare local variables within a block of code. In general, the scope of a local variable extends from its declaration to the end of the code block in which it was declared. In
Parameters are formal arguments to methods or constructors and are used to pass values into methods and constructors. The scope of a parameter is the entire method or constructor for which it is a parameter.
Exception-handler parameters are similar to parameters but are arguments to an exception handler rather than to a method or a constructor. The scope of an exception-handler parameter is the code block between
Consider the following code sample:
if (...) {
int i = 17;
...
}
System.out.println("The value of i + i); // error
The final line won't compile because the local variable MaxVariablesDemosourceIcon (in a .java source file), all of the variables
declared within the main method
are local variables.
The scope of each variable in that program extends from the declaration of the variable to the end of the main method
--indicated by the first right curly bracket }
in the program code.
{ and } that follow a catch statement.
Handling Errors with ExceptionstutorialIcon (in the Learning the Java Language trail) talks about using exceptions to handle errors and shows you how to write an exception handler that has a parameter.
i is out of scope. The scope of i is the block of code between the { and }. The i variable does not exist anymore after the closing }. Either the variable declaration needs to be moved outside of the if statement block, or the println method call needs to be moved into the if statement block.
| variableNamesnutsandbolts variableInitialization |
Bhopal news
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100