0

I saw some synthetic fields in class org.jfree.data.time.RegularTimePeriod, and have no ideas what they are and are for. I use this code to find them out:

for (Field f : RegularTimePeriod.class.getDeclaredFields())
    if (f.isSynthetic()) System.out.println(f);

And it will give these:

static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$java$util$Date
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$java$util$TimeZone
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Year
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Quarter
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Month
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Day
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Hour
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Minute
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Second
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Millisecond

Any body have any idea? I am just curious :) Thanks.

1 Answers1

1

As far as I know, synthetic members are only meant to be accessed by trusted code generated by the compiler, not haphazardly by reflection.

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.

A synthetic field pointing to the outermost enclosing instance is named this$0. The next-outermost enclosing instance is this$1, and so forth. (At most one such field is necessary in any given inner class.) A synthetic field containing a copy of a constant v is named val$v. These fields are final.

All these synthetic fields are initialized by constructor parameters, which have the same names as the fields they initialize. If one of the parameters is the innermost enclosing instance, it is the first. All such constructor parameters are deemed to be synthetic. If the compiler determines that the synthetic field's value is used only in the code of the constructor, it may omit the field itself, and use only the parameter to implement variable references.

A non-private final synthetic method which grants access to a private member or constructor has a name of the form access$N, where N is a decimal numeral. The organization of such access protocols is unspecified.

I hope this helps.

Cheers

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 1
    It is good practice to quote [your source](http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html). – assylias May 13 '13 at 08:31