您的位置:首页 > 移动开发 > Objective-C

Process of creating an object in Java

2007-06-13 15:32 519 查看
 summarize the process of creating an object. Consider a class called Dog:
The first time an object of type Dog is created (the constructor is actually a static method), 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.

As D
4000
og.class
is loaded (creating a Class object, which you’ll 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.

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

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.

Any initializations that occur at the point of field definition are executed.

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

以Class Dog为例说明初始化顺序,

1. 第一次生成Dog类型对象时,或者第一次访问到该类的static方法或成员变量时,Java 解释器就根据类文件路径找到Dog.class所在,载入类。

(Y-the compiled code for each class exists in its own separate file.“class code is loaded at the point of first use.”

编译后类代码存在一个单独的文件中,只有在第一次使用时才会被载入)

2.载入该类时,所有的static变量会进行一次性的初始化。

(Y-In the process of loading it, the loader notices that it has a base class (that’s what the extends keyword says), which it then loads. This will happen whether or not you’re going to make an object of that base class. If the base class has a base class, that second base class would then be loaded, and so on. Next, the static initialization in the root base class is performed, and then the next derived class, and so on. This is important because the derived-class static initialization might depend on the base class member being initialized properly. At this point, the necessary classes have all been loaded so the object can be created.

载入类时,loader发现类定义中有extends关键字,不管是否需要生成基类对象都需要载入extends后的基类,以此类推载入所有需要的类。然后,最根部基类的static变量被初始化,然后由下而上初始化static变量。这一点非常重要,因为可能子类的static变量初始化会依赖基类的static变量。现在载入完成,可以生成对象了)

3.new Dog时,构造函数会先在堆上给该类对象分配足够的存储空间。

4.该存储块清零,自动初始化成员变量为对应类型的默认初始值。

(Y-all the primitives in this object are set to their default values and the object references are set to null—this happens in one fell swoop by setting the memory in the object to binary zero.)

5.与C++不同的是,Java允许类中变量定义时能够设定初始值,在这一步,就是执行此类变量初始化。

6.执行构造函数。(Y-the base-class constructor will be called. In this case the call is automatic, but you can also specify the base-class constructor call by using super. The base class construction goes through the same process in the same order as the derived-class constructor. After the base-class constructor completes, the instance variables are initialized in textual order. Finally, the rest of the body of the constructor is executed. )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐