“静态”到底是什么?声明“全局”时的意思是Java 中的变量?

发布于 2024-09-13 04:51:31 字数 493 浏览 10 评论 0原文

我已经多次遇到这个问题,但我从来没有费心去了解为什么会发生这种情况,也没有去了解“静态”的实际含义。我刚刚应用了 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 
}

所以 eclipse 告诉我做 static int iNumVertices; 我不知道为什么。那么“静态”到底是什么,它是如何使用的,使用“静态”的目的是什么,为什么它会给我这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

£烟消云散 2024-09-20 04:51:31

这是您的示例:

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

    }
}

方法 main 是与该类关联的静态方法。它不与 Member 实例关联,因此它无法访问与 Member 实例关联的变量。解决方案是将这些字段设为静态。相反,您需要使用 new 关键字创建 Member 的实例。

这是修改后的版本:

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

发现自己在创建全局静态数据表明您应该仔细考虑如何设计某些东西。这并不总是错误的,但它应该告诉您思考您正在做的事情。

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.

决绝 2024-09-20 04:51:31

静态变量是在类的所有对象之间共享的变量。在您的示例中,对于您创建的每个 Member 对象,您将获得具有自己的 iNumVertices 值的对象。当您将static与变量一起使用时,Member 的每个对象之间仅共享一个变量。 静态方法的工作方式相同 - 它们在所有对象之间共享。

由于静态变量/方法对于所有对象都是通用的,因此不需要创建类的对象来访问这些变量/方法。

iNumVertices这样的非静态变量属于类的对象。如果不创建对象,则无法访问它们。因此,当您从静态上下文(这里是 main 方法)访问非静态变量时,java 将不知道您正在尝试访问哪个对象的 iNumVertices 。因此出现了错误。

要么使 iNumVertices 静态,要么通过创建 Member 对象来引用它

Member m = new Member();
m.iNumVertices = 0;

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;
月亮是我掰弯的 2024-09-20 04:51:31
learn what "static" actually means

静态实际上意味着类变量对于该特定类的所有实例都是相同的,但是如果您想避免使用静态变量(这是一个好主意,因为静态变量保存在内存中),您可以通过构造函数传递变量值从而避免使用 static 修饰符,并达到相同的效果(即在类实例化时传递相同的值)。

这是代码示例:

public class Car{

    private int speed;

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

}

因此您可以在创建新实例时执行此操作:

Car car = new Car(100);

每次创建 Car 实例时,它的速度都会为 100,从而避免静态声明 private static int speed = 100;

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;

十年不长 2024-09-20 04:51:31

静态变量是类变量。该变量的单个副本可供该类的所有实例使用,并且它们将共享该变量。静态成员也可以在不引用类的特定实例的情况下使用。

更多信息请参见:

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

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

甜味拾荒者 2024-09-20 04:51:31

静态变量不需要实例化类才能被访问,因此,如果您尝试从静态上下文访问非静态变量,则您将面临尝试访问尚未初始化/实例化的内容的风险。

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.

っ〆星空下的拥抱 2024-09-20 04:51:31

静态方法只能访问静态变量。类中有两种变量。一种是静态变量(也是类变量),另一种是实例变量。内存中只存在静态变量的一份副本,但将为每个对象实例化实例变量。因此,对于静态变量,所有对象都访问同一变量,一个对象所做的任何更改都将反映到其他对象。
这里的问题是为什么方法必须是静态的才能访问静态变量。当您将方法设置为静态时,您可以访问该方法而无需实例化该类的对象。那么,如果这个方法能够访问实例变量,那么它应该更改哪个对象的变量呢?另一种方式是可能的,即非静态方法可以访问静态变量。

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.

赴月观长安 2024-09-20 04:51:31

静态方法中使用的每个类变量(在类体内部和方法体外部声明的变量)也需要声明为静态。

静态类变量和方法可以在该类外部访问,而无需该类的实例。

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.

暖伴 2024-09-20 04:51:31

静态变量对于类的所有实例都是通用的。

注意:如前所述,这些是类变量,即由所有实例共享。

这些也可以称为类级别变量。
通常,您将常量(您还需要final关键字来定义常量)和全局变量定义为静态。

欲了解更多信息,请参阅:
http://download.oracle.com/javase/tutorial/java/ javaOO/classvars.html

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文