The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

## Scope

> A variable's scope is the block of code within which the variable is accessible and determines when the variable is created and destroyed. The location of the variable declaration within your program establishes its scope and places it into one of these four categories:
> - Member variable
> - Local variable
> - Method parameter
> - Exception-handler parameter

> A member variable is a member of a class or an object. It can be declared anywhere in a class but not in a method. The member is available to all code in the class. For information about declaring member variables, refer to Declaring Member Variables in the next lesson.

> You can declare local variables anywhere in a method or within a block of code in a method. In `MaxVariables`, all of the variables declared within the `main` method are local variables. The scope of each variable--the code that can access each variable--extends from the declaration of the variable to the end of the `main` method (indicated by the first right curly bracket `}` that appears in the program code). In general, a local variable is accessible from its declaration to the end of the code block in which it was declared.

> Method parameters are formal arguments to methods and constructors and are used to pass values into methods and constructions. The discussion about writing methods in the next lesson, Implementing Methods, talks about passing values into methods through parameters. The scope of a method parameter is the entire method or constructor for which it is a parameter.

> Exception-handler parameters are similar to method parameters but are arguments to an exception handler rather than to a method or a constructor. Handling Errors with Exceptions talks about using Java exceptions to handle errors and shows you how to write an exception handler that has a parameter.

| Start of Tutorial > Start of Trail > Start of Lesson | Search