Java Scope — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 22 / 50 · தமிழ்

Java Scope

Know where a name exists.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. 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.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

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.

02 / Step by step

விரிவான விளக்கங்கள்

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 ஆகும்.

Java reference fragmentJAVA
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: 0

Scope Summary

Scope LevelDeclared inAccessible inLifespan
Class (field)Class body, outside methodsAll methods of the classAs long as the object exists
MethodDirectly inside a methodAnywhere in that method (after declaration)Until method returns
BlockInside { } (if, for, while…)Only inside that blockUntil block ends
LoopFor-loop headerOnly inside the loop bodyDestroyed when loop ends

Java-வில் local variable-ஐ எங்கிருந்து access செய்யலாம்?

  1. Class-ல் எங்கும்
  2. Program-ல் எங்கும்
  3. Declare செய்த block/method-க்குள் மட்டும்
  4. அதே 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' என்பார்கள்.

03 / Read and predict

முழுமையான உதாரணம்

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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.