java 这是什么意思:“私人”修饰符与 JLS 建议不符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34887278/
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-11-02 23:30:23  来源:igfitidea点击:

what does this mean: 'private' modifier out of order with the JLS suggestions

javaeclipse-pluginfieldwarnings

提问by sisyphus

I first got this warning before adding 'final' to the myItem declaration:

在将“final”添加到 myItem 声明之前,我首先收到此警告:

Private field 'myItem' could be made final; it is only initialized in the declaration or constructor.

私有字段“myItem”可以是最终的;它仅在声明或构造函数中初始化。

private Item myItem;

After adding final this is the warning i get:

添加 final 后,这是我得到的警告:

'private' modifier out of order with the JLS suggestions.

'private' 修饰符与 JLS 建议不符。

final private Item myItem;

Does anyone know why I'm getting this? I've done some research but can't seem to find anything to solve this problem.

有谁知道我为什么会得到这个?我做了一些研究,但似乎找不到任何东西来解决这个问题。

回答by sisyphus

For the first, it's just good practice to make things finalif they definitely don't change during their lifetime. This helps for reasoning about the mutability of your objects.

首先,final如果它们在一生中绝对不会改变,那么制造它们只是一种很好的做法。这有助于推理对象的可变性。

For the second warning you're looking for JLSsections 8.1.1, 8.3.1 and 8.4.1.

对于第二个警告,您正在寻找JLS第 8.1.1、8.3.1 和 8.4.1 节。

publicor privateshould come before static, which should come before final.

publicprivate应先于static,应先于final

回答by dnault

The usual order is for privateto come before final, like this:

通常的顺序是 for privateto come before final,像这样:

private final Item myItem;

The code will still compile and behave the same if the modifiers are in a different order, but people who are used to the standard order suggested by the JLS will think your code looks weird.

如果修饰符的顺序不同,代码仍将编译并表现相同,但习惯了 JLS 建议的标准顺序的人会认为您的代码看起来很奇怪。

回答by Xin Meng

The reason for this warning is the order of different type of modifier, please check the answer of this question.

这个警告的原因是不同类型修饰符的顺序,请检查这个问题的答案

For this case, just put the access modifier before the final.

对于这种情况,只需将访问修饰符放在 final 之前。