简体   繁体   English

如何从同一类中的方法引用变量

[英]How do I refer to a variable from a method in the same class

I surprisingly find this confusing. 我惊讶地发现这令人困惑。 I must be missing something. 我肯定错过了什么。

So I have this simple syntax 所以我有这个简单的语法

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

Obviously this cause an error, since hi is a local variable. 显然这会导致错误,因为hi是局部变量。

Judging from my experience from python I added this 根据我从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;
    }
}

Which adds extra error when non-static variable cannot be accessed from a static method. 当无法从静态方法访问非静态变量时,会增加额外的错误。

I added static to hi 我给static加了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;
    }
}

The compiler scolds me for a illegal expression. 编译器骂我非法表达。 I substitute the static with private (which some SO answers, recommend) but same error. 我用private代替静态(有些SO答案,推荐)但是同样的错误。

Where's my mistake? 哪里是我的错? Is there any way I can solve this, without making global class? 有没有什么方法可以解决这个问题,而无需进行全球课程?

You cannot declare static variables inside a method because static modifier means that a method or field belongs to the class. 您不能在方法中声明static变量,因为static修饰符意味着方法或字段属于该类。

The easiest solution to this problem would be to declare the variable as static class variable. 解决此问题的最简单方法是将变量声明为static类变量。 Using this approach, you also need to remove this from this.hi in lestDoIt method. 使用这种方法,您还需要在lestDoIt方法中从this.hi中删除this The code would be like 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;
    }
}

Another solution may be using a non static variable hi . 另一种解决方案可以是使用非static变量hi This would need you to also remove the static modifier to the letsDoIt method to access to hi field, because static methods cannot access to instance fields because, as explained above, static means that a method or field belongs to the class and not to a specific object instance of the class. 这还需要你去除letsDoIt方法的static修饰符来访问hi字段,因为static方法无法访问实例字段,因为如上所述, static意味着方法或字段属于类而不属于特定该类的对象实例。

The solution would be: 解决方案是:

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;
    }
}

More info: 更多信息:

Static variables are variables of the class, not its instances. 静态变量是类的变量,而不是其实例。 You can't have a static variable inside a method. 您不能在方法中包含静态变量。

To fix this error, move hi outside the main method (keeping it static). 要修正这个错误,将hi的主要方法外(保持它的静态)。 Also get rid of the this in letsDoIt() . 也摆脱了thisletsDoIt()

public class OMG {

    static int hi=2;

    public static void main(String args[]){
        letsDoIt();
        System.out.println(hi);
    }

    public static void letsDoIt() {
        hi+=1;
    }
}

There are two elements causing your issue. 导致您出现问题的原因有两个。

  • variable hi must be referenced within a shared context between your main method and your letsDoIt method 变量hi必须在main方法和letsDoIt方法之间的共享上下文中引用
  • because your main method is static , and so your letsDoIt , the will only have visibility on static fields (unless passed an instance as argument, that is) 因为你的main方法是static ,所以你的letsDoIt ,只会对static字段有可见性(除非作为参数传递实例,即)

Hence: 因此:

  • Declare hi as a static field of OMG : hi声明为OMGstatic字段:

     public class OMG { static int hi; ... 
  • Remove the local variable declaration in your main method. 删除main方法中的局部变量声明。

  • Reference it with OMG.hi or just hi within a static context, not with this.hi as this implies an instance of Main , which is not visible from a static context 参照它OMG.hi或只是hi一内static情况下, this.hi因为this意味着一个实例Main ,这是不可见的static背景

You can not do "this.hi+=1" in a static context and in order to access the hi variable from "letsDoIt()" you have to declare it as a class variable like I did in the code below: 您不能在静态上下文中执行“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;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Clojure引用同一包中的Java类? - How do you refer to a Java Class in the same Package from a Clojure? 如何从另一个类中的 void 方法调用变量 - How do I call a variable from a void method in another class 如何在main方法的静态方法下引用变量? - How can I refer to a variable under a static method in the main method? 如何从同一类的另一个方法中调用变量,以及如何调用该方法? - How do I call for variables from another method in same class, as well as call upon the method? 如何从封闭的外部 class 调用同名方法作为在匿名 class 中实现的方法? - How do I call a method of same name from an enclosing outer class as the one being implemented in an anonymous class? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 我如何从另一个类访问另一个类的void方法的变量 - How do i access variable of void method of other class from another class 如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据 - How do I read in a public static, with variable data from a method, from another class and get updated data 如何从声明了变量的方法调用的方法中引用方法范围的变量? - How to refer to a variable with method scope from a different method called from method in which variable is declared? 如何从类内部引用非最终变量“out”? - How to refer to non final variable “out” from inside class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM