Java Academy · Lesson

Scope & Lifetime

Understand where variables are visible (scope) and how long they exist (lifetime): blocks, loops, methods, fields, shadowing, and final parameters.

Lesson 3 of 38 steps

Scope & Lifetime is a free Java Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Scope vs lifetime

Scope = where a name (variable) is visible. Lifetime = how long the variable exists. You will see block scope, loop scope, method locals, class fields, and shadowing rules.

Block scope

Block scope: a variable declared inside braces is visible only inside those braces.

public class Main {
  public static void main(String[] args) {
    int a = 10; // visible in main
    {
      int b = 20; // visible only inside this block
      System.out.println("inside block: a=" + a + ", b=" + b);
    }
    System.out.println("outside block: a=" + a);
    // System.out.println(b); // would NOT compile: b is out of scope
  }
}

Loop scope

Loop scope: a loop index declared in the for header is visible only inside the loop body.

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i < 3; i = i + 1) {
      System.out.println("i in loop = " + i);
    }
    // System.out.println(i); // would NOT compile: i is scoped to the loop

    int j;
    for (j = 0; j < 2; j = j + 1) {
      System.out.println("j in loop = " + j);
    }
    System.out.println("j after loop = " + j); // OK: j declared outside
  }
}

Fields vs locals

Lifetime: class/static fields live across calls; locals are recreated on each call and then discarded when the method ends.

public class Main {
  // static field (class-level): lives for the program lifetime
  static int counter = 0;

  static void tick() {
    int local = 100; // local: created each call, dies when method returns
    counter = counter + 1;
    System.out.println("tick: counter=" + counter + ", local=" + local);
  }

  public static void main(String[] args) {
    tick();
    tick();
    tick();
    System.out.println("final counter=" + counter);
  }
}

Shadowing

Shadowing: an inner declaration (e.g., a parameter or local) can hide an outer name with the same identifier. Use ClassName.field (or this.field in instance methods) to access the hidden field.

public class Main {
  static String label = "FIELD"; // class field

  static void demo(String label) { // parameter shadows field name
    System.out.println("param label = " + label); // uses parameter
    {
      String labelInner = "INNER"; // new name in inner block
      System.out.println("inner block labelInner = " + labelInner);
      System.out.println("still param label = " + label);
    }
    System.out.println("field via class name = " + Main.label); // refer to field explicitly
  }

  public static void main(String[] args) {
    demo("PARAM");
  }
}

Final & pass-by-value

final parameters cannot be reassigned. Java is pass-by-value: reassigning the parameter does not change the callers variable.

public class Main {
  // final parameter cannot be reassigned inside the method
  static int inc(final int n) {
    // n = n + 1; // would NOT compile if uncommented
    return n + 1; // safe: return a new value
  }

  static void reassign(int x) {
    x = 999; // reassignes the local copy; caller variable is unchanged
  }

  public static void main(String[] args) {
    int a = 5;
    int b = inc(a);
    System.out.println("a=" + a + ", b=inc(a)=" + b);

    reassign(a);
    System.out.println("after reassign(a): a is still " + a);
  }
}

Scope/Lifetime check

Quick check: Which statement about scope/lifetime is TRUE?

Recap & next

Recap: You practiced block and loop scope, fields vs locals, shadowing and final parameters. Next: move on to design and reuse patterns in A2.2.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Scope & Lifetime” lesson free?

Yes — the full text of “Scope & Lifetime” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Scope & Lifetime”?

Understand where variables are visible (scope) and how long they exist (lifetime): blocks, loops, methods, fields, shadowing, and final parameters. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Scope & Lifetime” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Methods & Parameters
  2. Return Values & Overloading
  3. Scope & Lifetime
← Back to Java Academy