什么是“静态"? [英] What is "static"?

查看:11
本文介绍了什么是“静态"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用 Java 编程.

I'm beginning to program in Java.

public static void main(String[]args)

一本书说在这种情况下我应该使用静态,但没有明确说明为什么我应该或它意味着什么.

A book said that I should use static in this case, but doesn't clearly say why I should or what it means.

你能澄清一下吗?

推荐答案

static 的概念与某物是类的一部分还是对象(实例)的一部分有关.

The concept of static has to do with whether something is part of a class or an object (instance).

对于声明为 staticmain 方法,它表示 main 方法是一个类方法——一个方法是类的一部分,而不是对象的一部分.这意味着另一个类可以通过引用ClassName.method 来调用另一个类的类方法.例如,调用 MyClass 的 run 方法将通过以下方式完成:

In the case of the main method which is declared as static, it says that the main method is an class method -- a method that is part of a class, not part of an object. This means that another class could call a class method of another class, by referring to the ClassName.method. For example, invoking the run method of MyClass would be accomplished by:

MyClass.main(new String[]{"parameter1", "parameter2"});

另一方面,没有 static 修饰符的方法或字段意味着它是对象(或也称为实例")的一部分,而不是类的一部分.是通过方法或字段所属的特定对象的名称来引用的,而不是类名:

On the other hand, a method or field without the static modifier means that it is part of an object (or also called "instance") and not a part of a class. It is referred to by the name of the specific object to which the method or field belongs to, rather than the class name:

MyClass c1 = new MyClass();
c1.getInfo()     // "getInfo" is an instance method of the object "c1"

由于每个实例可能具有不同的值,因此不同对象中具有相同名称的方法或字段的值不一定必须相同:

As each instance could have different values, the values of a method or field with the same name in different objects don't necessarily have to be the same:

MyClass c1 = getAnotherInstance();
MyClass c2 = getAnotherInstance();

c1.value     // The field "value" for "c1" contains 10.
c2.value     // The field "value" for "c2" contains 12.
             // Because "c1" and "c2" are different instances, and 
             // "value" is an instance field, they can contain different
             // values.

结合实例变量和类变量两个概念.假设我们声明了一个包含实例和类变量和方法的新类:

Combining the two concepts of instance and class variables. Let's say we declare a new class which contains both instance and class variables and methods:

class AnotherClass {
    private int instanceVariable;
    private static int classVariable = 42;

    public int getInstanceVariable() {
        return instanceVariable;
    }

    public static int getClassVariable() {
        return classVariable;
    }

    public AnotherClass(int i) {
        instanceVariable = i;
    }
}

上面的类有一个实例变量instanceVariable,和一个用static修饰符声明的类变量classVariable.同样,有一个实例和类方法来检索值.

The above class has an instance variable instanceVariable, and a class variable classVariable which is declared with a static modifier. Similarly, there is a instance and class method to retrieve the values.

实例的构造函数接受一个值作为参数分配给实例变量.类变量被初始化为 42 并且永远不会改变.

The constructor for the instance takes a value to assign to the instance variable as the argument. The class variable is initialized to be 42 and never changed.

让我们实际使用上面的类,看看会发生什么:

Let's actually use the above class and see what happens:

AnotherClass ac1 = new AnotherClass(10);

ac1.getInstanceVariable();             // Returns "10"
AnotherClass.getClassVariable();       // Returns "42"

注意调用类和实例方法的不同方式.它们以名称AnotherClass 引用类或以名称ac1 引用实例的方式.让我们更进一步,看看这些方法的行为差异:

Notice the different ways the class and instance methods are called. The way they refer to the class by the name AnotherClass, or the instance by the name ac1. Let's go further and see the behavioral differences of the methods:

AnotherClass ac1 = new AnotherClass(10);
AnotherClass ac2 = new AnotherClass(20);

ac1.getInstanceVariable();             // Returns "10"
AnotherClass.getClassVariable();       // Returns "42"
ac2.getInstanceVariable();             // Returns "20"
AnotherClass.getClassVariable();       // Returns "42"

可以看出,实例变量是由对象(或实例")持有的变量,因此对该特定实例是唯一的,在本例中是ac1ac2.

As can be seen, an instance variable is one that is held by an object (or "instance"), therefore unique to that particular instance, which in this example is the objects referred to by ac1 and ac2.

另一方面,类变量仅对整个类是唯一的.为了更好地理解这一点,让我们向 AnotherClass 添加一个新方法:

A class variable on the other hand is only unique to that entire class. To get this point across even better, let's add a new method to the AnotherClass:

public int getClassVariableFromInstance() {
    return classVariable;
}

然后,运行以下命令:

AnotherClass ac1 = new AnotherClass(10);
AnotherClass ac2 = new AnotherClass(20);

ac1.getInstanceVariable();             // Returns "10"
ac1.getClassVariableFromInstance();    // Returns "42"
ac2.getInstanceVariable();             // Returns "20"
ac2.getClassVariableFromInstance();    // Returns "42"

虽然getClassVariableFromInstance是一个实例方法,通过引用实例ac1ac2可以看出,它们都返回相同的值,42.这是因为在这两个实例方法中,它们引用的是类方法 classVariable ,它是类所独有的,而不是实例——只有一个 classVariable 副本对于 AnotherClass 类.

Although getClassVariableFromInstance is an instance method, as can be seen by being invoked by referring to the instances ac1 and ac2, they both return the same value, 42. This is because in both instance methods, they refer to the class method classVariable which is unique to the class, not to the instance -- there is only a single copy of classVariable for the class AnotherClass.

我希望能够澄清 static 修饰符的用途.

I hope that some what clarifies what the static modifier is used for.

Java 教程 有一个名为 了解实例和类成员,这也分为两类变量和方法.

The Java Tutorials from Sun has a section called Understanding Instance and Class Members, which also goes into the two types of variables and methods.

这篇关于什么是“静态"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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