What are local variables in Java?

Local variables have the most limited scope. Such a variable is accessible only from the function or block in which it is declared. The local variable's scope is from the line they are declared on until the closing curly brace of the method or code block within which they are declared.

Every local variable declaration statement is immediately contained by a block ({ ... }). Local variable declaration statements may be intermixed freely with other kinds of statements in the block.

A local variable declaration can also appear in the header of a for statement. In this case it is executed in the same manner as if it were part of a local variable declaration statement.

Local variables declaration have one and only one final modifer can be used.

Local variables are not given default initial values. They must be initialized explicitly before they are used.

The scope of a variable determines where it can be used. Syntactically, scope of any variable is the code between the curly braces of where it is declared.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |