探索Java中隐藏的访问权限synthetic
首先参考一下 Java8 虚拟机规范中对The Synthetic Attribute的相关描述:
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, the class initialization method, and the Enum.values() and Enum.valueOf() methods.
The Synthetic attribute was introduced in JDK 1.1 to support nested classes and interfaces.
一个类的源码中未出现的成员必须使用synthetic
属性或者ACC_SYNTHETIC
标志,但例外是编译期所生成的默认构造函数、类初始化方法,以及Enum.values()
和Enum.valueOf()
。
首先我来尝试着编译出一些存在synthetic
访问权限的字节码:
public final class Singleton {
public static Singleton getInstance() {
return Holder.INSTANCE;
}
private