Java Scope
Know where a name exists.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Scope.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java Scope என்னன்னு தெரியுமா?
Scope என்பது variable எங்கே accessible-ஆ இருக்கும்னு define பண்றது.
Method Scope
Method-க்கு உள்ளே declare ஆன variable அந்த method-க்கு உள்ளே மட்டுமே accessible ஆகும்:
Block Scope
Block-க்கு ({}) உள்ளே declare ஆன variable அந்த block-க்கு உள்ளே மட்டுமே accessible ஆகும்:
Variable-ஐ use பண்றதுக்கு முன்னாடி, அது அந்த scope-ல இருக்கான்னு உறுதிப்படுத்திக்கோ — இல்லன்னா compile error வரும்.
Class Scope (Fields)
Class-ல declare ஆன variables (fields) எல்லா methods-லயும் accessible — object இருக்கும் வரை live ஆகும்.
class Counter {
int count = 0; // class-scope field
void increment() { count++; }
void reset() { count = 0; }
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 | Only inside the loop body | Destroyed when loop ends |
Java-வில் local variable-ஐ எங்கிருந்து access செய்யலாம்?
- Class-ல் எங்கும்
- Program-ல் எங்கும்
- Declare செய்த block/method-க்குள் மட்டும்
- அதே package-ல் எங்கும்
Reveal answer / விடை
Declare செய்த block/method-க்குள் மட்டும்
Local variables declared inside a method or block are only accessible within that block. Method ends → variable destroyed. இதை 'block scope' என்பார்கள்.
முழுமையான உதாரணம்
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: A variable declared inside a loop or conditional block is not automatically available after that block.
பாடத்தின் சுருக்கம்
- 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.