COSC: 11.6 Scope of variable/method definitions

Ace your homework & exams now with Quizwiz!

side effects

-If a method's local variable (including parameter) has the same name as a field, then in that method the name refers to the local item and the field is inaccessible. -If a method updates a field, the method has effects that go beyond it's parameters and return value, which unless done carefully can make program maintenance hard

11.6.2: Method declaration and definition. 1. A method can access any other method defined in the same class.

Solution: 1. True A method is visible to all other methods defined in the same class regardless of the order in which the methods were defined

11.6.1: Variable/method scope. 1. A local variable is defined inside a method, while a field is defined outside any method. 2. A local variable's scope extends from a method's opening brace to the method's closing brace. 3. If a method's local variable has the same name as a method parameter, the name will refer to the local variable. 4. If a method's local variable has the same name as a field, the name will refer to the local variable. 5. A method that changes the value of a field is sometimes said to have "side effects".

Solutions: 1. True fields should be used carefully, and not as a replacement for parameters. 2. False Not from the opening brace, but rather from the variable's definition, to the closing brace. 3. False A method can't itself define two items with the same name; the compiler will generate an error. 4. True Such naming can confuse a reader. Best to avoid such naming if possible. 5. True Unexpected side effects of a method can make program maintenance hard.

field

a variable defined within a class but outside any method is called a class member variable, in contrast to a local variable defined inside a method. -scope extends from the class's opening brace to the class's closing brace, and reaches into methods regardless of where the field is defined within the class.

method's scope

extends from the class's opening brace to the class's closing brace - can access any other method defined in the same class, regardless of the order in which the methods are defined.

global variables

sometimes called fields, in contrast to local variables

scope

the name of a defined variable or method item is only visible to part of a program - a variable defined in a method has a scope limited to inside that method -scope starts after the definition until the method's end


Related study sets

MGMT 6100 Final Review Questions

View Set

Chapter 8 Preventing and Controlling Infection

View Set

bio evolution lectures 18-23 (quizlet 3)

View Set

AP EURO HISTORY TEST PART TWO!!!

View Set

Final Exam Review Questions - Email Security

View Set