The JavaTM Tutorial

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

Trail: Learning the Java Language
Lesson: Language Basics

## Final Variables

> You can declare a variable in any scope to be final . The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages.

> To declare a final variable, use the `final` keyword in the variable declaration before the type:

> >     final int aFinalVar = 0;

> The previous statement declares a final variable and initializes it, all at once. Subsequent attempts to assign a value to `aFinalVar` result in a compiler error. You may, if necessary, defer initialization of a final local variable. Simply declare the local variable and initialize it later, like this:

> >     final int blankfinal;
> >     . . .
> >     blankfinal = 0;

> A final local variable that has been declared but not yet initialized is called a blank final. Again, once a final local variable has been initialized, it cannot be set, and any later attempts to assign a value to `blankfinal` result in a compile-time error.

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

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.