I was wondering how assertions were implemented. I found that javac
uses a static field $assertionsDisabled
. And I was curious what will happen if $assertionsDisabled
is already used.
public class Test {
static final boolean $assertionsDisabled = Math.random() < .5;
public static void main(String[] args) {
assert false;
}
}
I expected javac
to use another name instead, just like other cases of automatic name generation. However, ...
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
This lead me to the question: Is this compiler behavior standard-compliant or not? Does the compiler have difficulty to use another name?