

In short, the compiler has created package-private methods for accessing private attributes. Because no modifier is shown for the synthetic methods created by the compiler, we know that they are package level (or package-private). As the newly added getDate() method shows in that screen snapshot, modifiers such as public are included in javap output. The last screen snapshot brings up another observation.
Java reflection method code#
Even with getDate() provided, the compiler would have generated a synthetic method for accessing the date has the enclosing code been written to access the date attribute directly (as a property) rather than via the accessor method. The above screen snapshot demonstrates that the compiler did not need to generate a synthetic method for accessing the private Date attribute in the nested class because the enclosing class accessed that attribute via the provided getDate() method. Out.println("Date: " + nested.getDate())

What happens when the nested class provides an accessor for its private data that the enclosing class can use? That is demonstrated in the next code listing and in its output as shown in the next screen snapshot.ĭemonstrateSyntheticMethods.java with Nested Class Public Accessor for Private Data package dustin.examples In all cases of an enclosing class accessing its nested class's private data, a synthetic method was created to allow that access to happen. However, when all four private attributes of the nested class were accessed by the enclosing class, four corresponding synthetic methods were generated by the compiler ( access$100, access$200, access$300, and access$400). When only one of the nested class's private attributes was accessed by the enclosing class, only one synthetic method ( access$100) was created by the compiler. Out.println("Boolean: " + nested.highl圜onfidentialBoolean) Īs the previous two code snippets above and the associated images show, the Java compiler introduces synthetic methods on an as-needed basis. Out.println("Calendar: " + nested.highl圜onfidentialCalendar) Out.println("Int: " + nested.highl圜onfidentialInt) The next code example demonstrates doing just this and the screen snapshot following it proves that four synthetic methods are generated in that case.ĭemonstrateSyntheticMethods.java (Enclosing Class Invokes Four Nested Class Private Attributes) package dustin.examples If I change the enclosing class to access all private attributes of NestedClass, additional synthetic methods will be generated. Note that the synthetic method is only added for the single private attribute of the NestedClass that the enclosing class accesses. class file, the output is as shown in the following screen snapshot.Īs the above screen snapshot indicates, a synthetic method with the name access$100 has been created on the nested class NestedClass to provide its private String to the enclosing class. The above code compiles without incident. Private boolean highl圜onfidentialBoolean = true Private Calendar highl圜onfidentialCalendar = Calendar.getInstance() Private String highl圜onfidential = "Don't tell anyone about me" Out.println("String: " + nested.highl圜onfidential) New DemonstrateSyntheticMethods.NestedClass() Public static void main(final String arguments)ĭemonstrateSyntheticMethods.NestedClass nested = Public final class DemonstrateSyntheticMethods The next code sample indicates this situation.ĭemonstrateSyntheticMethods.java (Enclosing Class Invokes One Nested Class Private Attribute) package dustin.examples The Java compiler must create synthetic methods on nested classes when their attributes specified with the private modifier are accessed by the enclosing class. That method's documentation states that it returns "true if and only if this member was introduced by the compiler." I like that very short definition of "synthetic": a Java construct introduced by the compiler. The Java Language Specification ( section 13.1) states "Any constructs introduced by the compiler that do not have a corresponding construct in the source code must be marked as synthetic, except for default constructors and the class initialization method." Further clues as to the meaning of synthetic in Java can be found in the Javadoc documentation for Member.isSynthetic(). The post summarizes what a Java synthetic method is, how one can be created and identified, and the implications of Java synthetic methods on Java development. In this blog post, I look at the concept of Java synthetic methods.
