Java Scope
Where variables can be accessed
🔍 What is Scope?
Scope determines where variables can be accessed in your program. Variables declared in different locations have different visibility and lifetime within your Java code.
public class ScopeExample {
static int globalVar = 10; // Class scope
public static void method() {
int localVar = 5; // Method scope
}
}
Scope Rules:
globalVar: accessible everywhere in class
localVar: only accessible in method()
Key Scope Concepts
Local Scope
Variables inside methods or blocks
void method() {
int local = 5;
}
Class Scope
Variables accessible throughout class
class MyClass {
static int classVar = 10;
}
Block Scope
Variables inside curly braces { }
if (true) {
int blockVar = 3;
}
Lifetime
How long variables exist in memory
// Created and destroyed
// based on scope
🔹 Local Scope Example
Variables declared inside methods have local scope:
public class LocalScopeExample {
public static void method1() {
int localVar = 10; // Only exists in method1
System.out.println("Method1 - localVar: " + localVar);
}
public static void method2() {
int localVar = 20; // Different variable, same name
System.out.println("Method2 - localVar: " + localVar);
// This would cause an error:
// System.out.println(method1's localVar); // Can't access!
}
public static void main(String[] args) {
method1();
method2();
// This would cause an error:
// System.out.println(localVar); // Not accessible here!
}
}
Output:
Method1 - localVar: 10
Method2 - localVar: 20
🔹 Class Scope Example
Static variables have class scope and can be accessed by all methods:
public class ClassScopeExample {
// Class scope variables (accessible everywhere in class)
static int classCounter = 0;
static String className = "MyClass";
public static void incrementCounter() {
classCounter++; // Can access class variable
System.out.println("Counter incremented to: " + classCounter);
}
public static void displayInfo() {
System.out.println("Class: " + className); // Can access class variable
System.out.println("Current counter: " + classCounter);
}
public static void main(String[] args) {
displayInfo();
incrementCounter();
incrementCounter();
displayInfo();
}
}
Output:
Class: MyClass
Current counter: 0
Counter incremented to: 1
Counter incremented to: 2
Class: MyClass
Current counter: 2
🔹 Block Scope Example
Variables declared inside blocks (like if statements) have block scope:
public class BlockScopeExample {
public static void demonstrateBlockScope() {
int outerVar = 10;
System.out.println("Outer variable: " + outerVar);
if (true) {
int blockVar = 20; // Only exists inside this block
System.out.println("Inside block - outerVar: " + outerVar);
System.out.println("Inside block - blockVar: " + blockVar);
}
// This would cause an error:
// System.out.println(blockVar); // Not accessible outside block!
System.out.println("Outside block - outerVar: " + outerVar);
}
public static void main(String[] args) {
demonstrateBlockScope();
}
}
Output:
Outer variable: 10
Inside block - outerVar: 10
Inside block - blockVar: 20
Outside block - outerVar: 10
🔹 Scope Rules Summary
Understanding where variables can be accessed:
Scope Rules:
- Local variables: Only accessible within the method where declared
- Class variables (static): Accessible throughout the entire class
- Block variables: Only accessible within the block { } where declared
- Parameter variables: Only accessible within the method that receives them
public class ScopeRules {
static int classVar = 100; // Class scope
public static void exampleMethod(int parameter) { // Parameter scope
int localVar = 50; // Method scope
if (parameter > 0) {
int blockVar = 25; // Block scope
System.out.println("All variables accessible here:");
System.out.println("Class: " + classVar);
System.out.println("Parameter: " + parameter);
System.out.println("Local: " + localVar);
System.out.println("Block: " + blockVar);
}
// blockVar not accessible here
}
}