Scope Rules
- The scope of an identifier is the portion of the program in which the identifier can be referenced.
- There are 2 kinds of scope:
- class scope:
- begins with the opening brace, {, of the class definition;
- ends with the closing brace, }, of the class definition.
- class scope enables a class's methods to:
- directly invoke all of the class's methods;
- directly access all instance variables defined in the class
- block scope:
- begins with the identifier's declaration;
- ends with the closing brace, }, of the block.
- local variables declared at the beginning of a method have block scope, as do method parameters.
- If a local variable in a method has the same name as an instance variable, then the instance variable is hidden
until the block terminates (illustrated with an applet).