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.
Default value of Local variable
Local variables are not given default initial values. They must be initialized explicitly before they are used.
Modifier of local variable
Local variables declaration have one and only one final modifer can be used.
1. valid: final local variable means that it cannot be change
2. invalid: public/protected/private/static are invalid.
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.
Instance Variable : member variable/ fields of class
No comments:
Post a Comment