9.3. Class ScopeIn Section 7.16, we discussed method scope; now, we consider class scope. A class's instance variables and methods belong to its scope. Within a class's scope, class members are accessible to all of the class's methods and properties and can be referenced simply by name (without an object reference). Outside a class's scope, class members cannot be referenced directly by name. Those class members that are visible (such as Public members) can be accessed through a "handle" (i.e., members can be referenced via names of the form objectReferenceName.memberName or ClassName.memberName for Shared members). For example, line 56 of Fig. 9.2 accessess a Time object's method with the method call time.ToUniversalString(). If a variable is defined in a method, only that method can access the variable (i.e., the variable is a local variable of that method). Such variables have block scope. If a method defines a local variable that has the same name as an instance variable, the local variable hides the instance variable in that method's scopethis is called shadowing. A shadowed instance variable can be accessed in a method of that class by preceding its name with the keyword Me and the dot separator, as in Me.hourValue. Section 9.8 discusses keyword Me. |