🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #60

Java Scope


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:

Example

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    int x = 100;

    // Code here CAN use x
    System.out.println(x);
  }
}

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:

Example

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

    } // The block ends here

    // Code here CANNOT use x

  }
}

Note: A block of code can stand alone, or be part of an if, while, or for statement. In a for loop, the variable declared in the loop header (like int i = 0) only exists inside the loop.


Loop Scope

Variables declared inside a for loop only exist inside the loop:

Example

public class Main {
  public static void main(String[] args) {

    for (int i = 0; i < 5; i++) {
      System.out.println(i); // i is accessible here
    }

    // i is NOT accessible here
  }
}
  • 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.

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

Example

public class Main {
  public static void main(String[] args) {

    for (int i = 0; i < 3; i++) {
      System.out.println("Loop 1: " + i);
    }

    for (int i = 0; i < 2; i++) {
      System.out.println("Loop 2: " + i);
    }
  }
}

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java Scope – FAQs

Quick answers about learning Java Scope in Java.

This free note from CodingNow 2.0 explains Java Scope in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java Scope, is 100% free with no signup required.
With focused practice, most students grasp Java Scope in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now