How can synthetic fields be created in Java?
Can synthetic fields in java only be created at runtime?
If not: Is there a standard-compliant way to this at compile time (without changing some bytes in the class file)
解决方案
They're created by the compiler when "oddities" of the language require them. A simple example of this is using an inner class:
public class Test
{
class Inner
{
}
}
The Test.Inner class will have a synthetic field to represent the appropriate instance of the Test class.
We can extend this code slightly to show that field:
import java.lang.reflect.*;
public class Test
{
public static void main(String[] args)
{
for (Field field : Inner.class.getDeclaredFields())
{
System.out.println(field.getName() + ": " + field.isSynthetic());
}
}
class Inner
{
}
}
With my compiler, that prints:
this$0: true