What are synthetic fields in Java? [duplicate]
Asked Answered
S

1

25

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?

Satem answered 12/6, 2013 at 18:39 Comment(1)
#5223768 and #7768304Jocundity
O
25

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

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

Osullivan answered 12/6, 2013 at 18:41 Comment(4)
"A synthetic field is a compiler-created field that links a local inner class to a block's local variable or reference type parameter"...I do not understand the meaning of this line . A code example would help.Satem
You can look into this blog for code example.Osullivan
so if I am create a cloning thru reflection over fields, I must skip them right?Overlarge
@AquariusPower yes you shouldEnvenom

© 2022 - 2024 — McMap. All rights reserved.