How do I prevent Eclipse from adding the 'final' keyword to a member variable declaration?
Asked Answered
G

6

20

When I type this

private MyKlass myklass;

and hit 'save' in Eclipse, it becomes this:

private final MyKlass myklass;

How do I stop Eclipse from doing this?

Grobe answered 22/8, 2011 at 12:12 Comment(1)
I want the same thing, BUT just on some specific cases (where there is bytecode processing and the vars cannot be final!!). I don't want to change the global eclipse code formatting...Olmstead
P
22

You need to disable that option in your 'save actions'.

right click your project > properties, then go to java editor > save actions. go to 'configure', 'code style' tab, and you have it on the bottom ('private fields').

Perbunan answered 22/8, 2011 at 12:14 Comment(0)
R
16

Window - Preferences - Java - Editor - Save Actions - Configure... - Code Style - Use modifier final when possible

The same option can be found in Java - Code Style - Clean up.

Rathbun answered 22/8, 2011 at 12:17 Comment(0)
R
11

If you want to leave this feature enabled in general but turn it off for a specific field, I found a simple workaround. Just declare a protected method that assigns to this field.

protected void preventFinal() { field = null; }

Risorgimento answered 26/6, 2012 at 22:33 Comment(0)
B
4

You can go to Project --> Properties --> Java Editor --> Save actions , Uncheck "Enable project specific settings"

Backscratcher answered 22/8, 2011 at 12:14 Comment(0)
H
1

There is Section called Save Actions in Eclipse Window -> Preferences -> Java -> Editor. There You can Configure Aditional actions - > (tab) code style - > Variable declarations -> edit use of final keyword.

Hummingbird answered 22/8, 2011 at 12:17 Comment(0)
D
0

Initialize the field with null:

private File tempDir = null;
Dipietro answered 1/7, 2022 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.