為什么構(gòu)造器不能是abstract, static, final, native or synchronized的?
Posted on 2007-04-10 21:03 久城 閱讀(2130) 評(píng)論(5) 編輯 收藏 所屬分類(lèi): 翻譯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.
翻譯如下:
不同于方法,構(gòu)造器不能是abstract, static, final, native, strictfp, 或者synchronized的.
1.構(gòu)造器不是通過(guò)繼承得到的,所以沒(méi)有必要把它聲明為final的。
2.同理,一個(gè)抽象的構(gòu)造器將永遠(yuǎn)不會(huì)被實(shí)現(xiàn)。(所以也不能聲明為abstract的)
3.構(gòu)造器總是關(guān)聯(lián)一個(gè)對(duì)象而被調(diào)用,所以把它聲明為static是沒(méi)有意義的。
4.沒(méi)有實(shí)際的需要把構(gòu)造器定義成同步的,因?yàn)樗鼘?huì)在構(gòu)造的時(shí)候鎖住該對(duì)象,直到所有的構(gòu)造器完成它們的工作,這個(gè)構(gòu)造的過(guò)程對(duì)其它線(xiàn)程來(lái)說(shuō),通常是不可訪(fǎng)問(wèn)的。
5.沒(méi)有本地的構(gòu)造器是任意一種語(yǔ)言的設(shè)計(jì)選擇,這樣會(huì)使得在創(chuàng)建對(duì)象的過(guò)程中JVM實(shí)現(xiàn)很容易去校驗(yàn)父類(lèi)的構(gòu)造器是否總是被正確地調(diào)用了。
(本地化的方法情況特別復(fù)雜,所以JVM調(diào)用起來(lái)非常麻煩,需要考慮很多種情況,沒(méi)有native關(guān)鍵字的情況下,JVM實(shí)現(xiàn)起來(lái)比較容易。)
歡迎來(lái)訪(fǎng)!^.^!
本BLOG僅用于個(gè)人學(xué)習(xí)交流!
目的在于記錄個(gè)人成長(zhǎng).
所有文字均屬于個(gè)人理解.
如有錯(cuò)誤,望多多指教!不勝感激!