【问题标题】:synthetic static fields in java with type "java.lang.Class"java中具有“java.lang.Class”类型的合成静态字段
【发布时间】:2013-05-07 05:27:13
【问题描述】:

我在 org.jfree.data.time.RegularTimePeriod 类中看到了一些合成字段,但不知道它们的用途和用途。我使用此代码找出它们:

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

它会给出这些:

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

有人知道吗?我只是好奇:) 谢谢。

【问题讨论】:

标签: java reflection synthetic


【解决方案1】:

据我所知,synthetic members are only meant to be accessed by trusted code generated by the compiler, not haphazardly by reflection.

编译器综合某些隐藏字段和方法以实现名称范围。除非另有说明,否则这些字段是私有的,或者它们最多属于包范围。

指向最外层封闭实例的合成字段名为this$0。下一个最外面的封闭实例是this$1,依此类推。 (在任何给定的内部类中最多需要一个这样的字段。)包含常量v 的副本的合成字段被命名为val$v。这些字段是final

所有这些合成字段都由构造函数参数初始化,这些参数与它们初始化的字段名称相同。如果其中一个参数是最内层的封闭实例,则它是第一个。所有这些构造函数参数都被认为是合成的。如果编译器确定合成字段的值只在构造函数的代码中使用,它可能会省略字段本身,只使用参数来实现变量引用。

授予对私有成员或构造函数的访问权限的非私有最终合成方法的名称格式为 access$N,其中 N 是十进制数字。这种访问协议的组织是未指定的。

我希望这会有所帮助。

干杯

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2011-06-21
相关资源
最近更新 更多