Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 java的初始化的理解和疑惑
javalean





发贴: 20
积分: 0
于 2005-08-06 22:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
关于java的初始化,我看了一些书,有一些收获,也多了一些迷茫。到现在为止对java的初始化还没有一个整体上清楚的了解。下面我就自己看过的一些较为经典的介绍java初始化的资料列出来,以供大家参考学习。如果有那一位高手能给我一个“整体上清楚的”的java初始化的解答,我将感激不尽。

首先是《thinking in java》介绍的一个对象创建的过程。
//先来看《java编程思想》中文第三版(原书为《thinking in java》,Bruce Eckel 著)第112页的一段话:

总结一下对象的创建过程会有帮助,假设有个名为Dog的类:
1)当首次创建类型为Dog的对象时(构造器可以看成静态方法),或者这Dog类的静态方法或静态字段首先被创建时,java解释器必须查找类路径,以定位Dog.class文件。
2)然后载入Dog.class(后面有学到,这将创建一个Class对象),有关静态初始化的所有动作都会执行。因此,静态初始化只在Class对象首次加载时进行一次。
3)当用new Dog()创建对象的时候,首先将在堆上为Dog对象分配足够的存储空间。
4)这块存储区域会被清零,这就自动地将Dog对象中所有的基本类型数据都设置成缺省值(对数字来说是0,对布尔型合字符型也相同),而引用被设置成了null。
5)执行所有出现于字段定义处的初始话动作。
6) 执行构造器。正如将在第6章所看到的,这将可能会牵涉到很多动作,尤其是涉及继承的时候。

//再对照来看《thinking in java》(英文版第2版,Bruce Eckel著)第277页的这段话:

It's helpful to summarize the process of creating an object .Consider a clas called Dog:

1. The first time an object of type Dog is created,or the first time a static method or static field of class Dog is accessed,the java interpreter must locate Dog.class ,which it does by searching through the classpath.

2. As Dog.class is loaded (Creating a Class object ,which you will learn about later),all of its static initializers are run .Thus ,static initialization takes place only once,as the Class object is loaded for the first time.

3. When you create a new Dog(),the construction process for a Dog object first allocates enough storage for a Dog object on the heap.

4. This storage is wiped to zero,automatically setting all the primitives in that Dog object to their default values(zero for numbers and the equivalent for boolean and char ) and the references to null.

5. Any initialization that occur at the point of field difinition are executed.

6. Constructors are executed.As you shall see in Chapter 6,this might actually involve a fair amonut of activity ,espeicially when inheritance is involved.

比较了这两段后,我对第2部分产生了疑惑。“all of its static initializers are run ”这句话在上文中被翻译成“有关静态初始化的所有动作都会执行”。这个翻译是含糊的。“ initializers”似乎更应该理解为“初始化器”(请原谅我的杜撰),而不是“初始化的动作”。后来我搜了一下“java的初始化”,看到了一篇名为《深入了解java初始化》,作者的目的是讲述了<clinit>和<init>这个两个“内置初始化方法”(作者的说法)。后来我再java.sun.com上看了一些介绍<clinit>和<init>以及初始化的文章,由于本人英语较差。下面把看到的一些东西贴出来。

//文章来源:http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html#12174

3.9 Specially Named Initialization Methods
At the level of the Java virtual machine, every constructor (§2.12) appears as an instance initialization method that has the special name <init>.
This name is supplied by a compiler. Because the name <init> is not a valid identifier, it cannot be used directly in a program written in the Java programming language.
Instance initialization methods may be invoked only within the Java virtual machine by the invokespecial instruction, and they may be invoked only on uninitialized class instances.

An instance initialization method takes on the access permissions (§2.7.4) of the constructor from which it was derived.

A class or interface has at most one class or interface initialization method and is initialized (§2.17.4) by invoking that method.

The initialization method of a class or interface is static and takes no arguments. It has the special name <clinit>.

This name is supplied by a compiler.
Because the name <clinit> is not a valid identifier, it cannot be used directly in a program written in the Java programming language.

Class and interface initialization methods are invoked implicitly by the Java virtual machine; they are never invoked directly from any Java virtual machine instruction, but are invoked only indirectly as part of the class initialization process。

//文章来源:(没有记下来,是java.sun.com上讲解JVM的内容上有的,大家可以找一下。)
5.5 Initialization
Initialization of a class or interface consists of invoking its static initializers (§2.11) and the initializers for static fields (§2.9.2) declared in the class. This process is described in more detail in §2.17.4 and §2.17.5.

A class or interface may be initialized only as a result of:

The execution of any one of the Java virtual machine instructions new, getstatic, putstatic, or invokestatic that references the class or interface.

Each of these instructions corresponds to one of the conditions in §2.17.4.

All of the previously listed instructions reference a class directly or indirectly through either a field reference or a method reference.

Upon execution of a new instruction, the referenced class or interface is initialized if it has not been initialized already. Upon execution of a getstatic,putstatic,or invokestatic instruction, the class or interface that declared the resolved field or method is initialized if it has not been initialized already.

Invocation of certain reflective methods in the class library (§3.12), for example, in class Class or in package java.lang.reflect.

The initialization of one of its subclasses.

Its designation as the initial class at Java virtual machine start-up (§5.2).
Prior to initialization a class or interface must be linked, that is, verified, prepared, and optionally resolved.

这两篇文章应该是回答了引用的《thinking in java》中的那段话的第二部分的疑惑,也就是在”all of its static initializers are run“到底是怎么一回事。只是我本人英语太菜,没有能理解,要是那位高手很讲解一下“static intializers are run”到底是个怎样的机制,感激不尽。至于其中的最后3个部分的内容,我已经从《thinking in java》中得到了解答。


javalean edited on 2005-08-06 23:48


话题树型展开
人气 标题 作者 字数 发贴时间
4415 java的初始化的理解和疑惑 javalean 5253 2005-08-06 22:37

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923