Checkstyle上的类变量 [英] Checkstyle on class variable

查看:156
本文介绍了Checkstyle上的类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Test {
private int x = 1;

public void set(int x){
this.x = x;
}
}

在上述情况下,它调用将x声明为final然而,将x声明为final将会在构造函数中初始化错误。什么是catch?

解决方案

你不能更改静态最终字段的值。



如果您真的需要x静态,请将您的方法更改为

  public static void setX(int newX) {
[...]

请记住,在静态方法this不能使用。



这应该可以解决你的问题。


Check style says that for a private class variable "must be declared final".

class Test {
    private int x=1;

    public void set(int x) {
        this.x = x;
    }
}

In the above case it calls to declare x as final however declaring x as final would give error on initializing it in the constructor. What's the catch?

解决方案

you cannot change the value of a static final field.

if you really need x to be static, change your method to

public static void setX(int newX){
    [...]

keep in mind, that in static methods "this" cannot be used.

this should solve your problem.

这篇关于Checkstyle上的类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆