Variable scope and lifetime
Variable scope and lifetime
Section titled “Variable scope and lifetime”Scope defines where a variable can be accessed. Lifetime is how long the variable exists in memory.
Types of scope
Section titled “Types of scope”- Block scope – variables declared inside a block
{}are only visible within that block.
{ int x = 10; // scope starts here // x is accessible}// x is not accessible here- Method scope – parameters and local variables declared inside a method are only visible inside that method.
public void myMethod(int param) { // param scope is whole method int local = 5; // local scope is the method // both accessible here}-
Class scope – instance variables (non‑static) are accessible throughout the class (subject to access modifiers).
-
Static scope – static variables belong to the class and are accessible everywhere the class is.
Example of block scope:
public void demo() { int a = 1; if (a > 0) { int b = 2; // b's scope is only the if block System.out.println(a + b); } // System.out.println(b); // error: b not in scope}Shadowing: A variable in an inner scope can hide a variable in an outer scope.
int x = 5;{ int x = 10; // shadows outer x System.out.println(x); // prints 10}System.out.println(x); // prints 5Lifetime
Section titled “Lifetime”- Local variables – created when execution enters their block, destroyed when block exits.
- Instance variables – created when an object is instantiated, destroyed when object is garbage collected.
- Static variables – created when class is loaded, destroyed when class is unloaded (typically at program end).
Example:
public class LifetimeDemo { static int staticVar = 1; // lives for whole program int instanceVar = 2; // lives per object
public void method() { int localVar = 3; // lives only during method call // ... }}