Skip to content

Commit cef67ee

Browse files
committed
feat: Add Scope class to demonstrate variable scope and shadowing in Java
WHAT the code does: - Defines a `Scope` class to illustrate how variable scope works in Java. - Shows that variables declared outside a block can be reassigned inside it. - Demonstrates block-scoped variables that cannot be accessed outside the block. - Shows loop-scoped variables (`i` and `num` inside the `for` loop). - Provides a `random(int marks)` method with its own local scope. WHY this matters: - Helps understand **scope, shadowing, and lifetime of variables** in Java. - Reinforces that variables with the same name cannot be redeclared in the same scope. - Clarifies the difference between reassignment and re-declaration. - Teaches best practices for avoiding naming conflicts. HOW it works: 1. Declares `a`, `b`, and `name` in `main()`. 2. Inside a block: - Reassigns `a = 100`. - Defines a new block-scoped variable `c = 99`. - Changes `name` to `"Rahul"`. 3. After block: - Creates a new `c = 900` (different from block’s `c`). - Prints updated values of `a`, `name`, and `c`. 4. In the `for` loop: - `i` and `num` exist only inside the loop body. - `a` is reassigned to `10000` in each iteration. 5. `random()` method: - Defines local `num = 67`. - Prints both local `num` and parameter `marks`. Tips & gotchas: - Attempting to redeclare `a` inside the block would cause a compile error due to shadowing rules. - Variables declared in a block (`{}`) cannot be accessed outside of it. - Loop counters and local variables are destroyed once the block exits. - Good practice: use descriptive names to avoid shadowing confusion. Use-cases: - Educational example to teach **variable scope and shadowing**. - Helps beginners avoid common compile-time errors with redeclaration. - Foundation for understanding **memory management and lifetime** of variables. - Useful in debugging scoping issues in larger codebases. Short key: class-scope-variable-shadowing. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 98eb17b commit cef67ee

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Section10Methods/Methods 2.O/src/Scope.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@ public class Scope {
22
public static void main(String[] args) {
33
int a = 10;
44
int b = 20;
5-
String name = "Kunal";
5+
String name = "Determine a scope.";
6+
67
{
7-
// int a = 78; // already initialised outside the block in the same method, hence you cannot initialise again
8-
a = 100; // reassign the origin ref variable to some other value
9-
System.out.println(a);
10-
int c = 99;
11-
name = "Rahul";
12-
System.out.println(name);
13-
// values initialised in this block, will remain in block
8+
// int a = 78;
9+
// already initialised outside the block in the same method, hence you cannot initialise again.
10+
11+
a = 100;
12+
// reassign the origin ref variable to some other value
13+
System.out.println(a);
14+
15+
int c = 99;
16+
name = "Rahul";
17+
System.out.println(name);
18+
// values initialised in this block, will remain in block
1419
}
1520
int c = 900;
1621
System.out.println(a);
1722
System.out.println(name);
18-
// System.out.println(c); // cannot use outside the block
23+
System.out.println(c); // cannot use outside the block
1924

20-
// scoping in for loops
2125
for (int i = 0; i < 4; i++) {
22-
// System.out.println(i);
26+
// System.out.println(i);
2327
int num = 90;
2428
a = 10000;
2529
}
2630
System.out.println();
2731
}
28-
2932
static void random(int marks) {
3033
int num = 67;
3134
System.out.println(num);

0 commit comments

Comments
 (0)