title: Why doesn't a Java constant divided by zero produce compile time error?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2934063/is-1-0-a-legal-java-expression">Is 1/0 a legal Java expression?</a>  </p>
  </blockquote>
  
  
  
  <p>Why does this code compile?</p>
  
  <pre><code>class Compiles {
      public final static int A = 7/0;
      public final static int B = 10*3;
  
      public static void main(String[] args) {}
  }
  </code></pre>
  
  <p>If I take a look in the compiled class file, I can see that B has been evaluated to 30, and that A still is 7/0.</p>
  
  <p>As far as I understand the JSL an expression where you divide by zero is not a constant.</p>
  
  <p>Ref: <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.28" rel="nofollow noreferrer">JLS 15.28</a></p>
  
  <p>My above statement is due to this line:</p>
  
  <blockquote>
    <p>A compile-time constant expression is an expression denoting a value of primitive type</p>
  </blockquote>
  
  <p>Hence dividing by zero is not evaluated to a primitive value.</p>
  
  <p>What I really dont understand is why the compiler allows this anyway? Just to be clear, my code above crashes runtime with a "java.lang.ExceptionInInitializerError"</p>
  
  <p>As it seems to me the compiler threats any final static variable as a constant and evaluates it compile time. That means that the compiler already has tried to evaluate A, but since it was a division by zero it just let it go through. No compile time error. But this seems very very bizarre... The compiler knows it is a divide by zero and that it will crash runtime but nevertheless it doesn't flag a compile error! </p>
  
  <p>Can anyone explain to me why?</p>