Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does "static" mean when declaring "global" variables in Java?

I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on.

public class Member {

 // Global Variables
 int iNumVertices;
 int iNumEdges;

 public static void main(String[] args) {

  // do stuff

  iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

  // do more stuff

 } // main end 
}

So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using "static", and why is it giving me this problem?

like image 201
Hristo Avatar asked Aug 05 '10 14:08

Hristo


2 Answers

Here's your example:

public class Member {

    // Global Variables
    int iNumVertices;
    int iNumEdges;

    public static void main(String[] args) {

        // do stuff

        iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

    }
}

The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.

Here's a modified version:

public class Member {
    // Fields
    private int iNumVertices;
    private int iNumEdges;

    public Member(){
        // init the class
    }

    public static void main(String[] args) {
        Member member = new Member();
        member.iNumVertices = 0;
        // do more stuff
    }
}

Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.

like image 183
Jonathon Faust Avatar answered Sep 20 '22 18:09

Jonathon Faust


static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.

Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.

Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.

Either make iNumVertices static, or refer to it by creating an object of Member

Member m = new Member();
m.iNumVertices = 0;
like image 32
Ara Avatar answered Sep 21 '22 18:09

Ara