Java Scope

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 22 / 50 · English

Java Scope

Know where a name exists.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 for loop has its own block ({ ... }).
  • The variable i declared in the loop header (int i = 0) is only accessible inside that loop block.
  • Once the loop ends, i is 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.

Java reference fragmentJAVA
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: 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 header (int i = 0)Only inside the loop bodyEach iteration; destroyed when loop ends

You can safely reuse the same variable name (i, j, etc.) in different loops in the same method:

03 / Read and predict

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.

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

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.

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