In Java, variables are confined to the region where they are declared, known as their scope.
Variables declared within a method are accessible throughout the method’s entirety beyond the line of code where they’re declared.
|
A code block encompasses all the code enclosed within curly braces { }.
Variables declared within code blocks are only accessible within the block of code enclosed by curly braces, following the line where the variable was declared.
|
A code block can stand alone or be associated with an if, while, or for statement. In the case of for statements, variables declared within the statement itself are also accessible within the block’s scope. |