static关键字如何在Java中工作? [英] How does the static keyword work in Java?

查看:97
本文介绍了static关键字如何在Java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始阅读 Java教程,我有一个关于字段或变量的 static 关键字的问题。正如 Java 所述这里

I'm reading Java tutorials from the begining and I have a question about static keyword on fields or variables. As Java said here:


类变量(静态字段) 类变量是使用static修饰符声明的任何字段;这告诉编译器这个变量只有一个副本存在,无论该类被实例化多少次。定义特定类型自行车齿轮数的字段可以标记为静态,因为从概念上讲,相同数量的齿轮将适用于所有实例。

Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances.

有了这个,我想如果你有一个对象(在这种情况下,类 Bicycle 的一个实例)和一个字段里面的那个 static 然后,独立于你是否正在加入 bicycle1 bicycle2 ,其静态将具有相同值的字段。我错了还是我理解得很好?

With that, I guess that if you have an object (in this case, an instance of the class Bicycle) and a field inside of it that its static then, independently of if you are refearing to bicycle1 or bicycle2, the field that its static will have the same value. Am I wrong or I understand it well?

我的意思是,如果我有:

I mean, if I have:

Bicycle bicycle1 = new Bicycle();
Bicycle bicycle2 = new Bicycle();

并且在班级 Bicycle 我有一个 static 字段如:

and in the class Bicycle I have a static field like:

class Bicycle{
   static int gears;

   //Methods to set and get gears
}

bicycle1 中,我将齿轮值设置为7:

And in the bicycle1 I set the value of gears to seven:

bicycle1.setGears(7);

然后如果我试图获得 bicycle2 我应该得到与我在 bicycle1 上设置的相同的值,对吗?

then if I try to get the value of gears in bicycle2 I should get the same value as I set on bicycle1, right?

System.out.println(bicycle2.getGears()); //7

嗯,这是我怀疑的地方因为 Java 在我上面的引文中说:

Well, here is where I have the doubt because as Java said in the quote that I put above:


这告诉编译器只有一个副本存在的变量

this tells the compiler that there is exactly one copy of this variable in existence

此副本存储在何处?对象如何访问该副本?该副本何时创建?

Where is this copy stored? How the objects access to that copy? When is this copy created?

推荐答案

此副本存储在何处?

副本(静态变量)存储在Permanent Geneneration部分中,但如果使用Java8,则永久生成部分不再存在。
静态变量以及静态方法是反射数据的一部分 - 类相关数据而非实例相关。

The copy(static variable) is stored in the Permanent Geneneration section, but if you use Java8 the Permanent Generation section not longer exists. The static variables and also the static methods are part of the reflection data - class related data and not instance related.

对象如何访问该副本?

您创建的每个类(对象)实例都有对该类的引用。

Every instance of class(object) that you have created have a reference to the class.

什么时候创建了这个副本?

它是在加载类时在运行时创建的:这是由类加载器完成的首次引用类时的JVM。

It is created at runtime when the class is loaded: this is done by the classloader of the JVM when the class is first referenced.

静态变量属于类,而不属于类的实例。
所以,你的直觉是对的,不管你创建了多少个对象,你只有一个副本。

Static variables belong to the class, and not to instances of the class. So, your intuition is right, you have only one copy regardless of how many object you create.

你可以使用一个静态变量来访问类的名称,如下例所示:

You can access a static variable using the name of the class, like in this example:

class Static {

    static int staticField;

}

public class UseStatic {

    public static void main(String[] args) {

        System.out.println(Static.staticField);

    }
}

静态字段对于创建某种类常量非常有用。

The static fields are useful to create some kind of class constants.

最后,为了轻松初始化特定类的静态字段,您可以使用静态初始化块

Finally, to easily initialize a static field of a specific class you can use Static Initialization Blocks.

来源:关于java的大学课程, java official文件

Sources: university course on java, java official documentation

这篇关于static关键字如何在Java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆