Java Scope
Know where a name exists.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Scope.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Scope determines where a name can be used. A method parameter or local variable has a limited scope; fields belong to the class or its objects. When a local name shadows a field, qualify the field when clarity requires it.
Explore the details
Java Scope
In Java, variables are only accessible inside the region where they are created. This is called scope.
Method Scope
Variables declared directly inside a method are available anywhere in the method following the line of code in which they were declared:
Block Scope
A block of code refers to all of the code between curly braces { }.
Variables declared inside a block of code are only accessible by the code between the curly braces, and only after the line in which the variable was declared:
Loop Scope
Variables declared inside a for loop only exist inside the loop:
- The
forloop has its own block ({ ... }). - The variable
ideclared in the loop header (int i = 0) is only accessible inside that loop block. - Once the loop ends,
iis destroyed, so you can't use it outside.
Why this matters
Loop variables are not available outside the loop.
Class Scope (Fields)
Variables declared at the class level (outside any method) are called fields. They are accessible from any method in the class and persist as long as the object exists.
class Counter {
int count = 0; // class-scope field — visible in all methods
void increment() {
count++; // accesses the field
}
void reset() {
count = 0; // also accesses the same field
}
void show() {
System.out.println("Count: " + count);
}
}
Counter c = new Counter();
c.increment();
c.increment();
c.show(); // Count: 2
c.reset();
c.show(); // Count: 0Scope Summary
| Scope Level | Declared in | Accessible in | Lifespan |
|---|---|---|---|
| Class (field) | Class body, outside methods | All methods of the class | As long as the object exists |
| Method | Directly inside a method | Anywhere in that method (after declaration) | Until method returns |
| Block | Inside { } (if, for, while…) | Only inside that block | Until block ends |
| Loop | For-loop header (int i = 0) | Only inside the loop body | Each iteration; destroyed when loop ends |
You can safely reuse the same variable name (i, j, etc.) in different loops in the same method:
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Counter counter = new Counter();
counter.show();
}
}
class Counter {
int count = 10;
void show() {
int count = 2;
System.out.println("Local: " + count);
System.out.println("Field: " + this.count);
}
}Predict first. Then reveal the expected output / வெளியீடு
Local: 2 Field: 10
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Rename the local variable from count to localCount.
Reveal the answer and reasoning / விடை
The output stays the same, but the naming no longer hides the field name.
One mistake to avoid: A variable declared inside a loop or conditional block is not automatically available after that block.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.