A static synthetic method is created by compiler if inner class accesses the outer class's private members:
package com.logicbig.example.method;
import java.lang.reflect.Method;
public class IsSyntheticExample {
private String x = "5";
private class MyInnerClass {
private void process() {
System.out.println(x);
}
}
public static void main(String... args) {
for (Method m : IsSyntheticExample.class.getDeclaredMethods()) {
if (m.isSynthetic()) {
System.out.printf(" Synthetic method%s%n name:%s%n", m, m.getName());
}
}
}
}
Output
Synthetic methodstatic java.lang.String com.logicbig.example.method.IsSyntheticExample.access$000(com.logicbig.example.method.IsSyntheticExample)
name:access$000
package com.logicbig.example.method;
import java.lang.reflect.Method;
import java.math.BigDecimal;
public class IsSyntheticExample2 {
public static void main(String... args) {
for (Method m : BigDecimal.class.getDeclaredMethods()) {
if (m.isSynthetic()) {
System.out.printf(" Synthetic method%s%n name:%s %n", m, m.getName());
System.out.println("---");
}
}
}
}
Output
Synthetic methodpublic int java.math.BigDecimal.compareTo(java.lang.Object)
name:compareTo
---
Synthetic methodstatic java.math.BigInteger java.math.BigDecimal.access$000(java.math.BigDecimal)
name:access$000
---