Thinking in Java

来源:互联网 发布:电影没字幕翻译软件 编辑:程序博客网 时间:2024/05/17 09:15

    • 摘录自Thinking in Java
    • Static 注意点
    • 总结

摘录自Thinking in Java

Ordinarily, when you create a class you are describing how objects of that class look and how they will behave. You don’t actually get an object until you create one using new, and at that point storage is allocated and methods become available.
通常来说,当创建类时,就是在描述那个类的对象的外观与行为。除非用new创建哪个类的对象,否则,实际上并未获得任何对象。执行new来创建对象时,数据存储空间才被分配,其方法才被外接调用。

There are two situations in which this approach is not sufficient. One is if you want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created. The other is if you need a method that isn’t associated with any particular object of this class. That is, you need a method that you can call even if no objects are created.
有两种情形用上述方法是无法解决的。一种情形是,只想为某特定域分配单一存储空间,而不去考虑究竟要创建多少对象,甚至根本就不创建任何对象。另一种情形是,希望某个方法不与包含它的类的任何对象关联在一起。也就是说,即使没有创建对象,也能够调用这个方法。

You can achieve both of these effects with the static keyword. When you say something is static, it means that particular field or method is not tied to any particular object instance of that class. So even if you’ve never created an object of that class you can call a static method or access a static field. With ordinary, non-static fields and methods, you must create an object and use that object to access the field or method, since non-static fields and methods must know the particular object they are working with.
通过static关键字可以满足这两方面的需要。当声明一个事物是static时,就意味着这个域或方法不会与包含那个类的任何实例关联在一起。所以,即使从未创建某个类的任何对象,也可以调用其static方法或访问其static域。通常,对于non-static 域和方法s,你必须创建一个对象,并用它来访问数据或方法。因为non-static域和方法必须知道他们一起运作的特定对象。

Some object-oriented languages use the terms class data and class methods, meaning that the data and methods exist only for the class as a whole, and not for any particular objects of the class. Sometimes the Java literature uses these terms too.
有些面向对象语言采用类数据和类方法两个术语,代表那些数据和方法只是作为整个类而不是类的某个特定对象而存在的。有时,一些Java文献里也用到这两个术语。

To make a field or method static, you simply place the keyword before the definition. For example, the following produces a static field and initializes it:
只需将static关键字放在定义之前,就可以将字段或方法定义为static。例如,下面的代码就生成了一个static字段,并对其进行了初始化:

class StaticTest {  static int i = 47;}

Now even if you make two StaticTest objects, there will still be only one piece of storage for StaticTest.i. Both objects will share the same i. Consider:
现在你创建了两个StaticTest对象,StaticTest.i也只有一份储存空间,这两个对象共享同一个i。再看看下面代码:

StaticTest st1 = new StaticTest();StaticTest st2 = new StaticTest();

At this point, both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory.
在这里,st1.i和st2.i指向同一储存空间,因此它们具有相同的值47.

There are two ways to refer to a static variable. As the preceding example indicates, you can name it via an object, by saying, for example, st2.i. You can also refer to it directly through its class name, something you cannot do with a non-static member.
引用static变量有两种方法。如前例所示,可以通过一个对象去定位它,如st2.i;也可以通过其类名直接引用,而这对于非静态成员却不行。

StaticTest.i++;

The ++ operator adds one to the variable. At this point, both st1.i and st2.i will have the value 48.
其中,++运算符对变量进行递加操作。此时,st1.i和st2.i仍具有相同的值48.

Using the class name is the preferred way to refer to a static variable. Not only does it emphasize that variable’s static nature, but in some cases it gives the compiler better opportunities for optimization.
使用类名是引用static变量的首选方式。这不仅是因为它强调了变量static结构,而且在某些情况下它还为编译器进行优化提供了更好的机会。

Using the class name is the preferred way to refer to a static variable. Not only does it emphasize that variable’s static nature, but in some cases it gives the compiler better opportunities for optimization.
使用类名是引用static变量的首选方式。这不仅是因为它强调了变量static结构,而且在某些情况下它还为编译器进行优化提供了更好的机会。

Similar logic applies to static methods. You can refer to a static method either through an object as you can with any method, or with the special additional syntax ClassName.method( ). You define a static method in a similar way:
类似逻辑也应用于静态方法。既可以像其他方法一样,通过一个对象来引用某个静态方法,也可以通过特殊的语法形式ClassName.method()加以引用。定义静态方法的方式也与定义静态变量的方式相似。

class Incrementable {  static void increment() { StaticTest.i++; }}

You can see that the Incrementable method increment( ) increments the static data i using the ++ operator. You can call increment( ) in the typical way, through an object:
可以看到,Incrementable的increment()方法通过++运算符将静态数据i递加。可以采用典型的方式,通过对象来调用increment():

Incrementable sf = new Incrementable();sf.increment();

Or, because increment( ) is a static method, you can call it directly through its class:
或者,因为increment()是一个静态方法,所以也可以通过它的类直接调用:

Incrementable.increment();

Although static, when applied to a field, definitely changes the way the data is created (one for each class versus the non-static one for each object), when applied to a method it’s not so dramatic. An important use of static for methods is to allow you to call that method without creating an object. This is essential, as you will see, in defining the main( ) method that is the entry point for running an application.
尽管当static作用于某个字段时,肯定会改变数据创建的方式(因为一个static字段对每个类来说都只有一份存储空间,而对非static字段则是对每个对象有一个存储空间),但是如果static作用于某个方法,差别却没有那么大。static方法的一个重要用法就是在不创建任何对象的前提下就可以调用它。正如我们将会看到的那样,这一点定义对main()方法很重要,这个方法是运行一个应用时的入口点。

Static 注意点

  1. 它只能调用static变量和方法。
  2. 不能引用this、super。
  3. static变量在定义时必须要进行初始化,且初始化时间要早于非静态变量。

总结

static可以看做“全局”,只要用static修饰,就是在类被加载时就已经”准备好了”,也就是可以被使用或者已经被执行,都可以脱离对象而执行。反之,如果没有static,则必须要依赖于对象实例。

0 0
原创粉丝点击