具有相同名称的静态变量和参数

考虑以下代码:

public class myclasss {
   private static int somevar;

   public setSomeVar(int somevar) {
     somevar = somevar;
   }
}

Obviously the code somevar = somevar; does not make sense. Is it possible to set the variable somevar to somevar without changing the parameters name? (i.e. differentiate between the two variables)?

评论
  • 花裤衩
    花裤衩 回复

    是的,请限定静态变量的名称:

    myclasss.somevar = somevar;
    

    对于实例变量类似:

    private int anothervar;
    
    public setAnothervar(int anothervar) {
      this.anothervar = anothervar;
    }