26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Methods 275<br />

6.11 Scope Rules<br />

The scope of an identifier for a variable, reference or method is the portion of the program<br />

that can reference the identifier. A local variable or reference declared in a block can be<br />

used only in that block or in blocks nested within that block. The scopes for an identifier<br />

are class scope and block scope. There is also a special scope for labels used with the<br />

break and continue statements (introduced in Chapter 5, “Control Structures: Part<br />

2”). A label is visible only in the body of the repetition structure that immediately follows<br />

the label.<br />

Methods and instance variables of a class have class scope. Class scope begins at the<br />

opening left brace, {, of the class definition and terminates at the closing right brace, }, of<br />

the class definition. Class scope enables methods of a class <strong>to</strong> invoke directly all methods<br />

defined in that same class or inherited in<strong>to</strong> that class (such as the methods inherited in<strong>to</strong> our<br />

applets from class JApplet) and <strong>to</strong> access directly all instance variables defined in the<br />

class. In Chapter 8, we will see that static methods are an exception <strong>to</strong> this rule. In a<br />

sense, all instance variables and methods of a class are global <strong>to</strong> the methods of the class<br />

in which they are defined (i.e., the methods can modify the instance variables directly and<br />

invoke other methods of the class). [Note: One of the reasons we use mainly applets in this<br />

chapter is <strong>to</strong> simplify our discussions. We have not as yet introduced a true windowed<br />

application in which the methods of our application class will have access <strong>to</strong> all the other<br />

methods of the class and the instance variables of the class.]<br />

Identifiers declared inside a block have block scope. Block scope begins at the identifier’s<br />

declaration and ends at the terminating right brace (}) of the block. Local variables<br />

of a method have block scope, as do method parameters, which are also local<br />

variables of the method. Any block may contain variable or reference declarations. When<br />

blocks are nested in a method’s body and an identifier declared in an outer block has the<br />

same name as an identifier declared in an inner block, the compiler generates a syntax<br />

error stating that the variable is already defined. If a local variable in a method has the<br />

same name as an instance variable, the instance variable is “hidden” until the block terminates<br />

execution. In Chapter 8, we discuss how <strong>to</strong> access such “hidden” instance variables.<br />

Common <strong>Program</strong>ming Error 6.14<br />

Accidentally using the same name for an identifier in an inner block of a method as is used<br />

for an identifier in an outer block of the same method results in a syntax error from the compiler.<br />

6.14<br />

Good <strong>Program</strong>ming Practice 6.7<br />

Avoid local-variable names that hide instance-variable names. This can be accomplished by<br />

avoiding the use of duplicate identifiers in a class. 6.7<br />

The applet of Fig. 6.10 demonstrates scoping issues with instance variables and local<br />

variables.<br />

This example uses the applet’s start method (lines 27–40) for the first time.<br />

Remember, when an applet container loads an applet, the container first creates an<br />

instance of the applet class. It then calls the applet’s init, start and paint methods.<br />

Method start always is defined with the line shown on line 27 as its first line.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!