如何防止 Eclipse 将“final”关键字添加到成员变量声明中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7147261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:52:38  来源:igfitidea点击:

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

javaeclipsecode-formatting

提问by Boris

When I type this

当我输入这个

private MyKlass myklass;

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

并在 Eclipse 中点击“保存”,它变成了这样:

private final MyKlass myklass;

How do I stop Eclipse from doing this?

如何阻止 Eclipse 执行此操作?

回答by Eran Zimmerman

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').

右键单击您的项目 > 属性,然后转到 Java 编辑器 > 保存操作。转到“配置”、“代码样式”选项卡,然后将其放在底部(“私有字段”)。

回答by JB Nizet

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

窗口 - 首选项 - Java - 编辑器 - 保存操作 - 配置... - 代码样式 - 尽可能使用修饰符 final

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

可以在 Java - Code Style - Clean up 中找到相同的选项。

回答by Steven Sudit

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; }

protected void preventFinal() { field = null; }

回答by Kit Ho

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

您可以转到 Project --> Properties --> Java Editor --> Save actions ,取消选中“Enable project specific settings”

回答by Eduard

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.

在 Eclipse Window -> Preferences -> Java -> Editor 中有一个名为 Save Actions 的部分。在那里您可以配置附加操作->(选项卡)代码样式-> 变量声明-> 编辑最终关键字的使用。