简体   繁体   English

在Java中声明“全局”变量时,“静态”究竟意味着什么?

[英]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. 我刚刚应用了Eclipse建议并继续进行的更改。

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; 所以eclipse告诉我做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? 那究竟什么是“静态”,它是如何使用的,使用“静态”的目的是什么,为什么它会给我这个问题呢?

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. 方法main是与类关联的静态方法。 It is not associated with an instance of Member , so it cannot access variables that are associated with an instance of Member . 它与Member的实例无关,因此无法访问与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. 相反,您需要使用new关键字创建Member实例。

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. 这并不总是错的,但它应该告诉你要思考你在做什么。

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. 在您的示例中,对于您创建的成员的每个对象,您将获得具有自己的iNumVertices值的对象。 When you use static with a variable, there is only one variable shared across every object of Member . static与变量一起使用时,在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. iNumVertices这样的非静态变量属于类的对象。 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. 因此,当您从静态上下文(此处为main方法)访问非静态变量时,java将不知道您尝试访问哪个对象的iNumVertices Hence the error. 因此错误。

Either make iNumVertices static, or refer to it by creating an object of Member 要么使iNumVertices成为静态,要么通过创建Member的对象来引用它

Member m = new Member();
m.iNumVertices = 0;
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). 什么静态实际上意味着Class变量对于该特定类的所有实例都是相同的,但是如果你想避免使用静态变量(这是一个好主意,因为静态变量保存在内存中)你可以传递变量值trough构造函数从而避免使用静态修饰符,并实现相同的效果(即在类实例化时传递相同的值)。

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; 并且每次创建Car实例时它都会有100的速度,从而避免静态声明private static int speed = 100;

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 http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

静态变量不需要实例化类以便被访问,因此如果您尝试从静态上下文访问非静态变量,则可能会尝试访问尚未初始化/实例化的内容。

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. 另一种方式是可能的,即非静态方法可以访问静态变量。

static variables are common to all instances of a Class. 静态变量对于Class的所有实例都是通用的。

Note: As said earlier these are class variables ie 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. 通常,您定义常量(您还需要用于定义常量的final关键字)和全局变量作为静态。

For more information refer: http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html 有关更多信息,请参阅: http//download.oracle.com/javase/tutorial/java/javaOO/classvars.html

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

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. 可以在该类外部访问静态类变量和方法,而无需该类的实例。

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

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