gpt4 book ai didi

非静态变量访问静态变量的java checkstyle规则

转载 作者:搜寻专家 更新时间:2023-10-31 19:57:10 24 4
gpt4 key购买 nike

是否有 Checkstyle 规则可用于限制对静态变量和方法的非静态访问?

这应该引发警告:

instance.staticField = value;

Eclipse 对此有一个设置,但我想在构建时强制执行它。

最佳答案

我想您正在寻找的是使用 javac -Xlint:static -Werror toto.java

来自 documentation :

  • -Xlint:name : 启用警告名称。请参阅 Warnings That Can Be Enabled or Disabled 部分使用 -Xlint 选项获取警告列表,您可以使用此选项启用。

  • -Werror : 如果出现警告则终止编译。

我试过这个例子:

public class StaticTest {
public static String toto = "toto";

public static void main(String s[]) {
StaticTest st = new StaticTest();
st.toto="dfd";
}
}

输出为:

StaticTest.java:16: warning: [static] static variable should be qualified by type name,

StaticTest, instead of by an expression

   st.toto="dfd";

^

error: warnings found and -Werror specified 1 error 1 warning

关于非静态变量访问静态变量的java checkstyle规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com