10 January 2026

Java: Enhancing Scope Safety: The forget Keyword

OVERVIEW

FEATURE SUMMARY:
The forget keyword prevents further access to a variable, parameter, or field within a defined scope, attempts to access the variable in forbiden scope will result in a compile-time error.

MAJOR ADVANTAGE:
This change makes variable and resource lifetimes explicit and compiler-enforced, improving code clarity and predictability.

MAJOR BENEFITS:

  • Allows explicitly removing a variable from the active context (in terms of accessibility), which is currently:
    • impossible for final variables (only comments can be used),
    • impossible for method parameters (except assigning null to non-final references),
    • impossible for fields,
    • cumbersome for local variables, requiring artificial blocks (extra lines and indentation).
  • Makes it possible to explicitly declare that a variable should no longer be used or no longer represents valid data in the current scope.
  • Preserves code quality over time, avoiding degradation caused by = null assignments, comments-only conventions, or artificial scoping blocks.

MAJOR DISADVANTAGE:
The introduction of a new reserved keyword introduces potential source incompatibilities with existing codebases that define identifiers named forget.

ALTERNATIVES:
Java currently provides only scope-based lifetime control (blocks and try-with-resources). It lacks a general, explicit, and compiler-enforced mechanism to terminate variable usability at an arbitrary point within an existing scope.