【问题标题】:How do I refer to a variable from a method in the same class如何从同一类中的方法中引用变量
【发布时间】:2014-06-18 14:58:59
【问题描述】:

我惊讶地发现这令人困惑。我一定是错过了什么。

所以我有这个简单的语法

public class OMG{
    public static void main(String args[]){
        int hi=2;
        letsDoIt();
        System.out.println(hi);
    }
    public static void letsDoIt(){
        hi+=1;
    }
}

显然这会导致错误,因为 hi 是一个局部变量。

根据我从 python 的经验来看,我添加了this

public class OMG{
    public static void main(String args[]){
        int hi=2;
        letsDoIt();
        System.out.println(hi);
    }
    public static void letsDoIt(){
        this.hi+=1;
    }
}

当无法从静态方法访问非静态变量时,这会增加额外的错误。

我加了 static 到嗨

public class OMG{
    public static void main(String args[]){
        static int hi=2;
        letsDoIt();
        System.out.println(hi);
    }
    public static void letsDoIt(){
        this.hi+=1;
    }
}

编译器责骂我的表达式非法。我用private(一些SO回答,推荐)替换静态但同样的错误。

我的错误在哪里?有什么方法可以解决这个问题,而不需要进行全局类?

【问题讨论】:

  • 您必须将static int hi=2; 放在main 方法体之外。
  • 您应该从阅读 java 教程开始,而不是基于其他语言的语法。
  • 我建议您阅读scope 的概念。另见here。然后,您可能想了解关键字static 应用于方法时的含义。
  • @SotiriosDelimanolis 不幸的是,我找不到涵盖此内容的 Java 教程。我身边有一本 Java 书,但没有关于“这个”的内容,所以我在黑暗中做了一个尝试。
  • 您找不到涵盖关键字thisstatic 的书籍或教程?

标签: java class scope


【解决方案1】:

您不能在方法内声明static 变量,因为static 修饰符意味着方法或字段属于该类。

解决这个问题最简单的方法是将变量声明为static 类变量。使用这种方法,您还需要在lestDoIt 方法中从this.hi 中删除this。代码是这样的:

public class OMG {
    static int hi=2;
    public static void main(String args[]) {
        letsDoIt();
        System.out.println(hi);
    }
    public static void letsDoIt() {
        hi+=1;
    }
}

另一种解决方案可能是使用非static 变量hi。这将需要您还删除letsDoIt 方法的static 修饰符以访问hi 字段,因为static 方法无法访问实例字段,因为如上所述,static 表示方法或字段属于该类,而不属于该类的特定对象实例。

解决办法是:

public class OMG {
    int hi=2;
    public static void main(String args[]) {
        //note that we have to create a new instance of OMG
        //because, again, static methods cannot access to non-static methods/fields
        OMG omg = new OMG();
        omg.letsDoIt();
        System.out.println(omg.hi);
    }
    public void letsDoIt() {
        this.hi+=1;
    }
}

更多信息:

【讨论】:

    【解决方案2】:

    有两个因素导致您的问题。

    • 变量hi 必须在main 方法和letsDoIt 方法之间的共享上下文中引用
    • 因为你的main 方法是static,所以你的letsDoIt,只会在static 字段上可见(除非传递了一个实例作为参数)

    因此:

    • hi 声明为staticOMG 字段:

      public class OMG {
          static int hi;
          ...
      
    • 删除 main 方法中的局部变量声明。

    • static 上下文中使用OMG.hi 或仅使用hi 引用它,not 使用this.hi 作为this 表示Main 的实例,即在static 上下文中不可见

    【讨论】:

      【解决方案3】:

      您不能在静态上下文中执行“this.hi+=1”,为了从“letsDoIt()”访问 hi 变量,您必须像我在下面的代码中那样将其声明为类变量:

      public class OMG{
              public static int hi;
      
              public static void main(String args[]){
                  hi=2;
                  letsDoIt();
                  System.out.println(hi);
              }
              public static void letsDoIt(){
                  hi+=1;
              }
          }
      

      【讨论】:

      • @LuiggiMendoza 不,你不能。因为this 指的是实例,而在static 上下文中根本没有实例。它不会编译。
      • @BackSlash 哦对了,我忘了方法是static,我傻了:)。
      【解决方案4】:

      静态变量是类的变量,而不是它的实例。方法中不能有静态变量。

      要修复此错误,请将 hi 移到 main 方法之外(保持静态)。还要去掉letsDoIt()中的this

      public class OMG {
      
          static int hi=2;
      
          public static void main(String args[]){
              letsDoIt();
              System.out.println(hi);
          }
      
          public static void letsDoIt() {
              hi+=1;
          }
      }
      

      【讨论】:

      • 我不明白您为什么在另一个问题中对 Java 上的 static 关键字给出了更好的解释,但这里没有......
      • 同时删除letsDoIt方法中的this引用
      • @LuiggiMendoza 我不关注。我回答的另一个问题?你知道是哪一个吗?
      猜你喜欢
      • 2018-12-17
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      相关资源
      最近更新 更多