Java 中的合成字段是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17072899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are synthetic fields in Java?
提问by Geek
Can someone explain in an easy to understand way the importance of synthetic fields in Java. I recall reading it in context of non static inner classes where each such inner class instance maintains a reference to the enclosing class. Why are such references/fields called synthetic fields?
有人可以用一种易于理解的方式解释 Java 中合成字段的重要性。我记得在非静态内部类的上下文中阅读它,其中每个这样的内部类实例维护对封闭类的引用。为什么这样的引用/字段称为合成字段?
回答by NINCOMPOOP
A synthetic field is a compiler-created field that links a local inner class to a block's local variable or reference type parameter. The compiler synthesizes certain hidden fields and methods in order to implement the scoping of names. These fields are private unless noted otherwise, or they are at most of package scope. You can get more information hereand in JLS
合成字段是编译器创建的字段,它将局部内部类链接到块的局部变量或引用类型参数。编译器综合某些隐藏字段和方法以实现名称的范围。除非另有说明,否则这些字段是私有的,或者它们最多属于包范围。您可以在此处和JLS 中获取更多信息
A class member that does not appear in the source code must be marked using a Synthetic attribute, or else it must have its ACC_SYNTHETIC flag set. The only exceptions to this requirement are compiler-generated methods which are not considered implementation artifacts, namely the instance initialization method representing a default constructor of the Java programming language (§2.9), the class initialization method (§2.9), and the Enum.values() and Enum.valueOf() methods..
未出现在源代码中的类成员必须使用 Synthetic 属性进行标记,否则必须设置其 ACC_SYNTHETIC 标志。此要求的唯一例外是编译器生成的方法,它们不被视为实现工件,即表示 Java 编程语言的默认构造函数的实例初始化方法(第 2.9 节)、类初始化方法(第 2.9 节)和枚举。 values() 和 Enum.valueOf() 方法..