1

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?

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Realdeo
  • 449
  • 6
  • 19
  • 1
    You must put `static int hi=2;` out of the `main` method body. – BackSlash Jun 18 '14 at 15:00
  • 1
    You should start by reading a java tutorial and not base its syntax on some other language. – Sotirios Delimanolis Jun 18 '14 at 15:01
  • I suggest you read up on the concept of [scope](http://en.wikipedia.org/wiki/Scope_(computer_science)). See also [here](http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm). Then you might want to find out what the keyword `static` means when applied to a method. – 0xbe5077ed Jun 18 '14 at 15:01
  • @SotiriosDelimanolis Unfortunately, I failed to find a java tutorial which covers this. I have a Java book beside me, but nothing about "this", so I made a stab in the dark. – Realdeo Jun 18 '14 at 15:01
  • You couldn't find a book or tutorial that covered the keywords `this` and `static`? – Sotirios Delimanolis Jun 18 '14 at 15:03
  • I tried, there's 2 question on 'this'. It talks about 3 function of 'this' but left out a example of the function of ' this ' in this kind of example. – Realdeo Jun 18 '14 at 15:05
  • That's because your example is not an example of an application of `this`. You need to read between the lines. The tutorials will tell you the purpose of `this` and `static`. You apply it accordingly. – Sotirios Delimanolis Jun 18 '14 at 15:08
  • @Realdeo my answer covers how can you use `this` to solve the problem. – Luiggi Mendoza Jun 18 '14 at 15:09

4 Answers4

3

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:

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

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;
    }
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
0

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

Mena
  • 47,782
  • 11
  • 87
  • 106
0

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;
        }
    }
Rikku121
  • 2,536
  • 2
  • 23
  • 38