简体   繁体   中英

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.

Judging from my experience from python I added 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

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.

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.

The easiest solution to this problem would be to declare the variable as static class variable. Using this approach, you also need to remove this from this.hi in lestDoIt method. 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 . 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.

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). Also get rid of the this in letsDoIt() .

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
  • 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)

Hence:

  • Declare hi as a static field of OMG :

     public class OMG { static int hi; ... 
  • Remove the local variable declaration in your main method.

  • 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM