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

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

问题描述

我开始用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.

你能澄清一下吗?

推荐答案

<概念code> static 与某些东西是某个类或对象(实例)的一部分有关。

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

如果是 main 方法,声明为 static ,它表示 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 modifier意味着它是对象的一部分(或者也称为实例),而不是类的一部分。它由方法或字段所属的特定对象的名称引用,而不是类名:

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"

可以看出,实例变量是一个由object(或instance),因此对于该特定实例是唯一的,在此示例中是由 ac1 ac2

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 是一个实例方法,可以通过引用实例 ac1 ac2 来调用,它们都返回相同的值, 42 。这是因为在两种实例方法中,它们都引用了类方法 classVariable ,这对于类是唯一的,而不是实例 - 只有一个<$的副本c $ c> 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.

我希望有些什么澄清静态修饰符的用途。

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天全站免登陆