Skip to content

Latest commit

 

History

History
13 lines (9 loc) · 1.29 KB

variable_shadowing.md

File metadata and controls

13 lines (9 loc) · 1.29 KB

Variable Shadowing

Variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. At the level of identifiers (names, rather than variables), this is known as name masking. This outer variable is said to be shadowed by the inner variable, while the inner identifier is said to mask the outer identifier. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

Some languages allow variable shadowing in more cases than others. For example Kotlin allow an inner variable in a function to shadow a passed argument and a variable in inner block to shadow another in outer block, while Java does not allow these. Both languages allow a passed argument to a function/Method to shadow a Class Field.

Some languages, such as CoffeeScript, disallow variable shadowing completely.