无法解析私有方法中的变量

时间:2016-03-29 18:52:06

标签: java oop methods

我的IDE(intelliJ)告诉我它无法解决" this.x"中的变量x。我这样做了吗?

 private Double localTax(){
double x = 0;

    if(grossIncome <= 45000){
        x = (grossIncome * 0.0115);
    }
    else if (grossIncome > 45000){
        x = (45000 * 0.0115);
    }
    return this.x;
}

2 个答案:

答案 0 :(得分:3)

没有。 x是您方法的局部变量。 this指的是实例字段,而不是局部变量。删除this以获取要编译的代码。

答案 1 :(得分:0)

您正在设置局部变量x。

this.x指实例变量。

public class hello {
  private int x; //this is this.x

  public int foo(int globalIncome){
    int x = globalIncome;
    return x; //returns globalIncome
  }
}
相关问题