Variable Scope

Learn about different visibility ranges for variables.

Having learned to create and use methods, it’s time to learn about a concept called variable scope. Each variable is available within a specific context or scope. Outside this context, the variable is nonexistent and inaccessible.

There are various scope levels:

  • Class scope. Variables declared on the class level can be accessed from any method of this class.
  • Method scope. Variables declared within a method are accessible only within this method.
  • Block scope. Like variables within a method, block-level variables are local to their scope and can’t be accessed from an outside context.
Scope levels

Get hands-on with 1400+ tech skills courses.