Scope of Variables In Java
This session discusses Java scope, which is intended to meet the needs of learners.
Java Scope
It is important to note that variables in Java are only accessible within the region they were created in. We refer to this as scope.
Method Scope
Any variable declared directly inside a method is available anywhere following the line in which it was declared:
Method Scope Example: 
Example: 
Block Scope
All code between curly braces is considered a block of code. Variables declared inside blocks of code can be accessed only by the code between curly braces { }, following the line where the variable was declared:
Block Scope Example: 
Example: 
It is possible for a block of code to exist alone or as part of an if, while, or for statement. If you use for statements, variables declared inside the statement are also accessible inside the block.
Java Scope Key Points:
- A scope is generally defined by a set of curly brackets [ ].
- When we are writing Java code, variables are usually accessible if they are defined within the curly brackets or inside of the curly brackets where they are defined.
- A class variable can be used by all member methods even if it is not defined within a method.
- A class variable can be referenced by the “this” keyword when a method uses the same local variable as a member.
- To read a variable after a loop ends, it must be set before the loop body.