Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 300230

Summary: [clean up] 'Add final to local variables' only works if initialized at declaration
Product: [Eclipse Project] JDT Reporter: Brian Miller <Brian.Miller>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r, numeralnathan
Version: 3.5   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug

Description Brian Miller CLA 2010-01-20 12:53:31 EST
Build Identifier: 20090920-1017

class Bug {
	void method(){
		String value;
		if(equals(this)) {
			value="";
		} else {
			value=""+this;
		}
		value.toString();
	}
}

Reproducible: Always

Steps to Reproduce:
1. Configure the Java>Editor>SaveActions preference to include "Add final modifier to local variables".
2. Add some irrelevant whitespace to class Bug's editor.
3. Save class Bug's change.  See that variable 'value' was wrongly not automatically declared final.
Comment 1 Markus Keller CLA 2010-01-25 09:52:37 EST
The 'Add final modifier to local variables' clean up currently only works if the variable is initialized at the declaration (and not written later).

Fixing this would need much more analysis. From bug 272532 comment 3:

> To be honest, that whole clean up should be rewritten to just
> - create a working copy with all supported variables set to final
> - compile the working copy
> - create changes for those variables which didn't introduce a definite
> assignment problem
Comment 2 Nathan Reynolds CLA 2011-12-30 11:04:52 EST
With HotSpot 7u1, declaring a local variable or parameter final does *not* have a performance impact.  The optimizer is able to do all of the optimizations with or without the final keyword.  Declaring a field final *does* have a performance impact.
Comment 3 Eclipse Genie CLA 2019-08-10 19:19:27 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.