[フレーム]

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java this Keyword Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Enums Java User Input Java Date

Java Errors

Java Errors Java Debugging Java Exceptions

Java File Handling

Java Files Java Create/Write Files Java Read Files Java Delete Files

Java Data Structures

Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator

Java Advanced

Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting

Java How To's

Add Two Numbers Count Words Reverse a String Sum of Array Elements Convert String to Array Sort an Array Find Array Average Find Smallest Element ArrayList Loop HashMap Loop Loop Through an Enum Area of Rectangle Even or Odd Number Positive or Negative Square Root Random Number

Java Reference

Java Reference Java Keywords Java String Methods Java Math Methods Java Output Methods Java Arrays Methods Java ArrayList Methods Java LinkedList Methods Java HashMap Methods Java Scanner Methods Java Iterator Methods Java Errors & Exceptions

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Certificate


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);
 }
}

Try it Yourself »


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
 }
}

Try it Yourself »

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
 }
}

Try it Yourself »

  • 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);
  }
 }
}

Try it Yourself »


Class Scope

Variables declared inside a class but outside any method have class scope (also called fields). These variables can be accessed by all methods in the class:

Example

public class Main {
 int x = 5; // Class variable
 public static void main(String[] args) {
  Main myObj = new Main();
  System.out.println(myObj.x); // Accessible here
 }
}

Try it Yourself »

Note: You will learn more about classes and objects in the Java OOP chapters.




Track your progress - it's free!
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /