Java中的合成字段是什么? [英] What are synthetic fields in Java?

查看:316
本文介绍了Java中的合成字段是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用一种易于理解的方式解释Java中合成字段的重要性.我记得在非静态内部类的上下文中阅读过它,其中每个这样的内部类实例都维护对封闭类的引用.为什么将这些引用/字段称为合成字段?

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?

推荐答案

合成字段是编译器创建的字段,该字段将本地内部类链接到块的本地变量或引用类型参数.编译器综合某些隐藏字段和方法,以实现名称范围.除非另有说明,否则这些字段是私有的,或者它们最多属于软件包范围.您可以获取更多信息

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 here and in JLS

未出现在源代码中的类成员必须使用Synthetic属性进行标记,否则必须设置其ACC_SYNTHETIC标志.唯一的例外是编译器生成的方法,这些方法不被视为实现工件,即代表Java编程语言的默认构造函数的实例初始化方法(第2.9节),类初始化方法(第2.9节)和Enum. values()和Enum.valueOf()方法.

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..

这篇关于Java中的合成字段是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆