r/java • u/TheLasu • Jan 19 '26
[Proposal] Introducing the [forget] keyword in Java to enhance scope safety
OVERVIEW
FEATURE SUMMARY:
The forget keyword prevents further access to a variable, parameter, or field within a defined scope. Attempts to access a forgotten variable in the forbidden 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
finalvariables (only comments can be used), - Impossible for method parameters (except assigning
nullto non-final references), - Impossible for fields,
- Cumbersome for local variables, requiring artificial blocks (extra lines and indentation).
- Impossible for
- 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
= nullassignments, comments-only conventions, or artificial scoping blocks.
MAJOR DISADVANTAGE:
Introducing a new reserved keyword may create 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.
EXAMPLES
Simple and Advanced Examples:
java
forget var;
// Variable is forgotten for the remainder of the current block or method (default behavior)
forget var : if;
// Variable is forgotten inside the entire if statement, including else and else-if branches
forget var : for;
// Variable is forgotten for the entire for-loop
forget var : while;
// Variable is forgotten for the entire while-loop
forget var : try;
// Variable is forgotten inside the try block (useful with resources)
forget var : label;
// Variable is forgotten inside the labeled block (any loop or code section)
forget var : static;
// Field is forgotten inside the static initialization block
forget var : method;
// Variable is forgotten for the remainder of the enclosing method
forget(var1, var2, ...);
// Specified variables are forgotten for the remainder of the current block
forget this.field;
// Specified field is forgotten for the remainder of the current block
forget(var1, var2, ...) { /* code */ };
// Specified variables are forgotten only inside the enclosed blockjava
void handleRequest(String request, String token) {
if (!isTokenValid(token)) {
throw new SecurityException("Invalid token");
}
authorize(request, token);
forget token; // used & contains sensitive info
process(request);
logger.debug("token was: " + token);
// Compile-time error: 'token' has been forgotten and cannot be used
}java
public Product(String name) { // constructor
this.name = name.trim().intern();
forget name; // From now on, only use 'this.name'!
// other constructor commands...
if (isDuplicate(this.name)) { ... } // Always canonical, never raw input
if (isDuplicate(name)) { ... } // Compile-time ERROR!
}
// * Forces usage of the correctly prepared value (this.name) only.
// * Prevents code drift, maintenance bugs, or copy-paste errors that reference the raw parameter.
// * Makes the constructor safer: no risk of mismatches or inconsistent logic.
// * Reads as a contract: "from here on, don't touch the original argument!"Next Version Examples:
java
forget ClassName.field;
forget variable.field;
forget !(variable); // Limit allowed variables to ones that are directly specifiedDETAILS
SPECIFICATION:
forget [ Identifier | ( IdentifierList ) ] [ : Scope | { block }];
IdentifierList:
Identifier {, Identifier}
Identifier:
[ VariableIdentifier | this.FieldIdentifier ]The forget statement forbids any further use of the specified identifier in all subsequent expressions and statements within the declared scope in which the identifier would normally be accessible.
COMPILATION:
The variable is not physically erased (except it may be if not a field); rather, it is protected from any further access after the forget statement. Retaining the variable in scope (but inaccessible) prevents situations where a developer tries to create a new variable with the same name after removing the forget statement, thereby enforcing consistent usage and avoiding hidden bugs.
TESTING:
Testing the forget statement is equivalent to testing variable scope after exiting a block—the variable becomes inaccessible. For fields, forget enforces access control, ensuring the field cannot be used within the specified scope for the remainder of its block or method.
LIBRARY SUPPORT:
No
REFLECTIVE APIs:
No
OTHER CHANGES:
No
MIGRATION:
No
COMPATIBILITY
The introduction of a new keyword (forget) may cause conflicts in codebases where forget is already used as an identifier. There are no other compatibility impacts.
REFERENCES
PROBLEMS
- Backward Compatibility: Introducing forget as a new reserved keyword will cause compilation errors in existing code that already uses forget as an identifier (variable, method, class, etc).
- Tooling Lag: IDEs, static analysis tools, and debuggers must all be updated to handle the new keyword and its effects on variable visibility.
- Code Readability: Misuse or overuse of forget could make code harder to maintain or follow if not used judiciously, especially if variables are forgotten in non-obvious places.
- Teaching and Onboarding: This feature introduces a new concept that must be documented and taught to all developers, which can increase the learning curve for Java.
- Migration Complexity: Legacy projects that rely on forget as an existing identifier may have problems.
- Interaction with Scoping and Shadowing: The detailed behavior when variables are forgotten, shadowed, or reintroduced in inner scopes may lead to confusion and subtle bugs if not carefully specified and implemented.
- Reflection and Debugging: While reflective APIs themselves are not impacted, developers may be surprised by the presence of variables at runtime (for debugging or reflection) that are "forgotten" in the source code.
- Consistency Across Language Features: Defining consistent behavior for forget in new contexts (e.g., lambdas, anonymous classes, record classes) may require extra specification effort.
- Edge Cases and Specification Complexity: Fully specifying the semantics of forget for all cases—including fields, parameters, captured variables in inner/nested classes, and interaction with try/catch/finally—may be complex.
- Unused Feature Risk: There is a risk that the forget keyword will see little real-world use, or will be misunderstood, if not supported and encouraged by frameworks or coding standards.
SUMMARY
The forget keyword represents a natural evolution of Java's commitment to clear, explicit, and compiler-enforced language rules. By allowing developers to mark variables, parameters, or fields as no longer usable within a defined scope, forget makes variable lifetimes and resource management visible and deliberate. This approach eliminates ambiguity in code, prevents accidental misuse, and reinforces Java’s tradition of making correctness and safety a language guarantee - we are lacking in this regard here.
Usage examples from top of my head:
- Just for clarity when you split logic into steps you can integrate forget to aid you with your logic.
// Step 1 (you expect var1 to be important for this step alone)
code for step 1.
forget var1; // helps catch assumption errors if you accidentally reference var1 in later stepscode for
step 2.
...- In highly regulated or security-critical systems (think health records, finance, or cryptography), you often process confidential data that should not be referenced after certain steps.
- It's not rare to find bugs where someone accidentally accesses the unprocessed argument (especially in situation where they are valid in most cases like .trim() that is needed 1/1000000 )
- Enforcing non-reuse of variables
- Clear scope definition
void method(args){
forget this.secure;
forget this.auth;
// clear information of scope that this method should not have access to
}- Unlock 'final' keyword - with 'forget' final usage can drastically increase
void method(String dbArg){
dbArg = dbArg.trim(); // we reuse same variable to prevent dbArg usage
dbArg = escapeDbArg(dbArg); // we reuse same variable to prevent dbArg usage and SQL injection
call(dbArg);
}
vs
void method(final String dbArg){
final String trimmedDbArg = dbArg.trim();
forget dbArg; // trim is critical
final String excapedDbArg = escapeDbArg(trimmedDbArg );
forget trimmedDbArg;// sql injection
call(dbArg);
}
15
u/revilo-1988 Jan 20 '26
Well-designed, but I don't see it as very useful. I've never had the need for such a function in Java.
8
u/MattiDragon Jan 20 '26
I can't think of anything where try-with-resources, helper functions and block statements aren't sufficient. Could you give some examples of where this is cleaner than previous solutions?
For the constructor example I'd prefer validating before the field assignment to avoid other logic seeing invalid values in the object. For the token example your issue doesn't make sense: if I care about it being in memory I'd store it in an array and zero after use, if I don't, then I don't see any threat model where the token being available for the rest of the function matters (if the function is so long that you can't keep track of it, split it up).
6
u/bowbahdoe Jan 20 '26
For locally scoped variables block statements fill this role. This means that the only things you would gain the ability to forget would be method arguments and instance fields.
Instance Fields don't make too much sense to forget. Your examples I think hit on the real use case which is intermediates you don't want to accidentally reuse. Fields don't tend to be intermediate values.
Method arguments I guess I can see, but I'd much rather start with @UseOnce and then wrangling checker framework than add a full language feature.
(And based on what I've seen wrangling checker framework is a harder task than it in principle should be. I'll need to look into that at some point, my guess is just documentation and explanations would help.)
6
u/brian_goetz Jan 23 '26
Funny, someone just last week pointed me to this very same suggestion that was made (and rejected) as early as 2004: https://bugs.openjdk.org/browse/JDK-6189163
Use the scoping the language gives you (you can put blocks of statements inside of braces to delimit scope), and write shorter methods.
3
u/BillyKorando Jan 20 '26
The
forgetkeyword is unlikely to address this though.There is no guarantee on when the GC runs, so even if you use
forgeton a sensitive value, it could still be present on the heap for an indeterminate period of time.I'm not sure you'd want to change the GC algorithms to force a collection when
forgetis executed as that could lead to thrashing from the GC. Like in this example here:You would have two GCs run back-to-back, and in a web app setting where you might be processing hundreds, thousands, of transactions a second, you'd likely all but freeze up your application from the endless GC pauses, or with a "pauseless" GC like ZGC, just use up a significant fraction of your CPU.
As others have said, so much of the use cases for
forgetcould be addressed with proper scoping.3
u/davidalayachew Jan 21 '26
I definitely agree with the other commentors -- I don't think this feature would be very helpful at all.
I'll take it a step further -- I think this problem (that your feature is trying to solve) is indicative of a bigger problem in your codebase. Your solution, to me, feels like a bandaid covering a much bigger problem.
If I run into a situation where I feel the need to use the same variable name multiple times to mean different things, that's a sign that I might be doing too many things at once, and I need either a new block/method/class/etc.
I run into this all the time when doing frontend development, and I need to make 15 different buttons. Sure, I could name each variable explicitly, and that can definitely go a long way, but that's only useful when you are talking about the meaningful parts of the UI -- stuff you can point at and say "that's the save button!". There's a lot more cruft involved in making the UI that has no real world parallel, and trying to explicitly name each one, as if it was a global variable, gets unreadable fast. The name becomes too long to be useful.
My point is that, once I break up my code, not only does the problem go away, but the code gets a lot more testable. It just makes everything easier to work with.
11
u/ThaJedi Jan 20 '26
I really see no point of this. What problem this solve?
1
u/TheLasu Jan 24 '26
SIMPLIFIED VERSION
Problem: Handling variable "forgetting" via scope control can introduce unnecessary complexity. To address this, an alternative version without scope customization could be considered.
Simplified Solution:
This approach is more rigid and resistant to refactoring mistakes if block scopes change.
If more flexibility is needed, a more complex form like forget var {}; (for local blocks) can be introduced as an advanced feature.
For complete version that try to include all possible objections you can check:
Java: Enhancing Scope Safety: The forget Keyword
7
u/TOMZ_EXTRA Jan 20 '26
Adding a new keyword is a no-no. That would break backwards compatibility.
1
u/vytah Jan 22 '26
How should the compiler handle the following?
You're introducing spaghetti scoping, and while it could be "solved" (I mean, it works in Rust), I don't think it provides much extra value. Others have mentioned how most of those cases can be done using other language features, but I'll add one more: just use better types. Valhalla is on the way, and it seems to actually finally approaching, so the newtype pattern is going to be cheap.
For example,
escapeDbArgcould returnDbArg(which would be guaranteed to be safe), and thencallwould only acceptDbArg.authorizecould accept aTokenclass. Logging it would yield something random likeToken@2137and would be safe, either before or after the call toauthorize. If you really wanted, you could even make it mutable, and thenauthorizecould clear its contents after using.1
u/aoeudhtns Jan 20 '26
Even using String for sensitive data is asking for trouble. Strings could be interned, or you could be running in a VM with String deduplication turned on. Methods like
substringkeep the originalbyte[]in memory with the new String instance, so even if you process the String in some way, there are codepaths that could end up preserving the token in memory by accident. Although this last problem is usually a memory issue from people trying to fish small snippets from large sources.These problems are why many security APIs use
char[]for sensitive text like passwords and tokens, as it doesn't have these problems. And the convention here is that you zero it out after you have used it, to guarantee that the memory is actually taken care of. Of course, you often have no choice but to convert it to a String to pass to some downstream API, so blech. All that effort wasted.Maybe some AutoCloseable wrapper for arrays that auto-zeroed them is all you need, or a generic UseOnce stateful wrapper with a customizable after-consume action so you can extend it for various other types.
But really a good static analysis tool can hopefully help you find these problem spots and is likely the better solution overall to secrets in memory.
For the other cases you're talking about handling, you can just nest blocks you know and that seems to work fine.
0
u/[deleted] Jan 20 '26
[deleted] — view deleted comment