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

瘦欲@ 提交于 2019-12-19 17:39:57

问题


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?


回答1:


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.




回答2:


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;



回答3:


learn what "static" actually means

What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).

Here is code example :

public class Car{

    private int speed;

    public Car(int speed){
        this.speed = speed;
    }

}

So you can do this when creating new instance :

Car car = new Car(100);

and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;




回答4:


Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.

More here:

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html




回答5:


Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.




回答6:


Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects. The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.




回答7:


Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.

Static class variables and methods can be accessed outside that class without the need for an instance of that class.




回答8:


static variables are common to all instances of a Class.

Note: As said earlier these are class variables i.e. shared by all instances.

These can also be called as class level variables. Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.

For more information refer: http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

http://www.roseindia.net/java/beginners/staticvariable.shtml



来源:https://stackoverflow.com/questions/3415781/what-exactly-does-static-mean-when-declaring-global-variables-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!