首页 > 代码库 > 为什么构造器不能是abstract, static, final, native or synchronized的?

为什么构造器不能是abstract, static, final, native or synchronized的?

Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized.

1. A constructor is not inherited, so there is no need to declare it final

2. As the same reason above, an abstract constructor could never be implemented.

3. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.

4. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work.

5. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java virtual machine to verify that superclass constructors are always properly invoked during object creation.

翻译如下:

不同于方法,构造器不能是abstract, static, final, native, strictfp, 或者synchronized的.

1.构造器不是通过继承得到的,所以没有必要把它声明为final的。

2.同理,一个抽象的构造器将永远不会被实现。(所以也不能声明为abstract的)

3.构造器总是关联一个对象而被调用,所以把它声明为static是没有意义的。

4.没有实际的需要把构造器定义成同步的,因为它将会在构造的时候锁住该对象,直到所有的构造器完成它们的工作,这个构造的过程对其它线程来说,通常是不可访问的。

5.没有本地的构造器是任意一种语言的设计选择,这样会使得在创建对象的过程中JVM实现很容易去校验父类的构造器是否总是被正确地调用了。

(本地化的方法情况特别复杂,所以JVM调用起来非常麻烦,需要考虑很多种情况,没有native关键字的情况下,JVM实现起来比较容易。)

 

 

转自:http://www.blogjava.net/realsmy/archive/2007/04/10/109256.html

 

为什么构造器不能是abstract, static, final, native or synchronized的?