淺談Java編程中的synthetic關(guān)鍵字
java synthetic關(guān)鍵字。有synthetic標(biāo)記的field和method是class內(nèi)部使用的,正常的源代碼里不會出現(xiàn)synthetic field。小穎編譯工具用的就是jad.所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。
下面我給大家介紹一下synthetic
下面的例子是最常見的synthetic field
Java代碼
class parent {
public void foo() {
}
class inner {
inner() {
foo();
}
}
}
非static的inner class里面都會有一個this$0的字段保存它的父對象。編譯后的inner class 就像下面這樣:
Java代碼
class parent$inner{
synthetic parent this$0;
parent$inner(parent this$0)
{
this.this$0 = this$0;
this$0.foo();
}
}
所有父對象的非私有成員都通過 this$0來訪問。
還有許多用到synthetic的地方。比如使用了assert 關(guān)鍵字的class會有一個synthetic static boolean $assertionsDisabled 字段
使用了assert的地方
assert condition;
在class里被編譯成
Java代碼
if(!$assertionsDisabled && !condition){
throw new AssertionError();
}
還有,在jvm里,所有class的私有成員都不允許在其他類里訪問,包括它的inner class。在java語言里inner class是可以訪問父類的私有成員的。在class里是用如下的方法實(shí)現(xiàn)的:
Java代碼
class parent{
private int value = 0;
synthetic static int access$000(parent obj)
{
return value;
}
}
在inner class里通過access$000來訪問value字段。
synthetic的概念
According to the JVM Spec: "A class member that does not appear in the source code must be marked using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces."
I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug.
Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too.
注意上文的第一處黑體部分,一個類的復(fù)合屬性表示他支持嵌套的類或者接口。
注意上文的第二處黑體部分,說明符合這個概念就是OO思想中的類的復(fù)合,也就是只要含有其它類的引用即為復(fù)合。
總結(jié)
以上就是本文關(guān)于Java編程中的synthetic關(guān)鍵字的全部內(nèi)容,希望對大家能有所幫助。感謝大家對本站的支持。
相關(guān)文章
Springboot使用異步請求提高系統(tǒng)的吞吐量詳解
這篇文章主要介紹了Springboot使用異步請求提高系統(tǒng)的吞吐量詳解,和同步請求相對,異步不需要等待響應(yīng),隨時可以發(fā)送下一次請求,如果是同步請求,需要將信息填寫完整,再發(fā)送請求,服務(wù)器響應(yīng)填寫是否正確,再做修改,需要的朋友可以參考下2023-08-08
JavaSE實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇博客是在學(xué)習(xí)了一部分Java基礎(chǔ)語法之后的練習(xí)項(xiàng)目,通過這個小項(xiàng)目的練習(xí),對Java中的類和對象,抽象類和接口等進(jìn)行熟悉理解。快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java中?SLF4J和Logback和Log4j和Logging的區(qū)別與聯(lián)系
這篇文章主要介紹了Java中?SLF4J和Logback和Log4j和Logging的區(qū)別與聯(lián)系,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考幾種,感興趣的小伙伴可以參考一下2022-09-09
解決SpringBoot連接SqlServer出現(xiàn)的問題
在嘗試通過SSL與SQL?Server建立安全連接時,如果遇到“PKIX?path?building?failed”錯誤,可能是因?yàn)槲茨苷_配置或信任服務(wù)器證書,當(dāng)"Encrypt"屬性設(shè)置為"true"且"trustServerCertificate"屬性設(shè)置為"false"時,要求驅(qū)動程序使用安全套接字層(SSL)加密與SQL?Server建立連接2024-10-10

