【发布时间】:2015-03-01 09:47:18
【问题描述】:
我想知道断言是如何实现的。我发现javac 使用了一个静态字段$assertionsDisabled。我很好奇如果 $assertionsDisabled 已经被使用会发生什么。
public class Test {
static final boolean $assertionsDisabled = Math.random() < .5;
public static void main(String[] args) {
assert false;
}
}
我希望javac 使用另一个名称,就像其他自动名称生成的情况一样。不过……
C:\Users\...\src>javac -J-showversion Test.java
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
Test.java:1: error: the symbol $assertionsDisabled conflicts with a compiler-syn
thesized symbol in Test
public class Test {
^
Test.java:2: error: the symbol $assertionsDisabled conflicts with a compiler-syn
thesized symbol in Test
static final boolean $assertionsDisabled = Math.random() < .5;
^
2 errors
这让我想到了一个问题:这种编译器行为是否符合standard 标准?编译器使用其他名字有困难吗?
【问题讨论】:
标签: java compiler-errors assertions standards-compliance name-collision