Is there a way to stop Intellij from creating final fields

Answered

Hello,

I often use the 'Create Field' refactoring in IntelliJ and really like it. Unfortunately if you us it the constructor most of the time you end up with a final field, even though you now you will reassign the value later on. 

Example:

You start with a nice new class like:

public class MyNewClass {

public MyNewClass() {

myList = Collections.emptyList();
}
}

Than place the cursor on `myList` hit Alt+Enter select:

Create field 'myList' in 'MyNewClass'

Hit Enter again and you end up with:

public class MyNewClass {

private final List<Object> myList;

public MyNewClass() {

myList = Collections.emptyList();
}
}

and then you can move to the field declaration and remove the final keyword, before creating getter and setter for `myList`.

Is there a way to stop IntelliJ form making the fields final? I know that option for variables and parameters but not for fields.

 

Thanks and Regards,

Maik

0
6 comments

Hi.

Please check Settings | Editor | Code Style | Java | Code Generation | Make generated local variables final.

0
Avatar
Permanently deleted user

Hey Petr,

thanks for your answer! As I mentioned, I have this option disabled and IMHO it doesn't effect class fields.

0

Hello. Looks like this feature is not implemented yet, please upvote https://youtrack.jetbrains.com/issue/IDEA-31585

0
Avatar
Permanently deleted user

Thanks for the clarification!

 

I placed upvoted the mentioned issue.

0
Avatar
Permanently deleted user

Please please do provide a workaround. This issue's driving me a bit crazy.

0
Avatar
Permanently deleted user

Found solution.

1. Settings | Editor | Code Style | Java | Code Generation | Make generated local variables final. -> Untick. (didn't help but I kept it unticked anyway)
2. Generate field using alt + enter - a pop-up window appears and you untick declare final, then hit TAB


RESULT:
Your field generation is completed without final.
From now on it works for all generated fields just fine.

idea version:

3

Please sign in to leave a comment.