课程: Java Essential Training: Syntax and Structure

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Variable scope

Variable scope

- [Instructor] Variables are only valid within the scope in which they are defined. And by scope, you can think of it as the immediate curly braces that a variable was declared in is the limit to where it can be utilized. For example, in method1, we have declared a variable named myVariable. Because this variable was defined within the curly braces of method1, this method is the only place in which it can be used. If we try to reference myVariable from method2, we get a compilation error indicating that the symbol cannot be resolved. This is because myVariable is not known outside of the scopre of method1 and therefore cannot be referenced anywhere but within method1. Scope can be even narrower than the method level. We could also limit a variable's scope to any block of code. For example, here on line four, we defined the myVariable variable within an if decision structure. Remember, a variable scope is…

内容