Java Scope Tutorial With Examples
Define :
- A variable defined within a scope is available only to the end of that scope.
- Most procedural languages have the concept of
- scope.
- This determines both the visibility and lifetime of the names defined within that scope.
- In C, C++, and Java, scope is determined by the placement of curly braces {}.
- We will understand scope with an example -
Example of block scope

Note - As we know a method is also a kind of block so scope of a variable inside a block is same as the scope of a variable inside a method.
Scope of a Object -
Java objects do not have the same lifetimes as primitives. When you create a Java object using new. We will understand with an example

- the object ‘s’ vanishes at the end of the scope.
- However, the String object that s was pointing to is still occupying memory. In this bit of code, there is no way to access the object after the end of the scope, because the only reference to it is out of scope.