title: Final, immutable objects are not constants?
body: <p>The class <code>Integer</code> is a wrapper of the <code>int</code> primitive type (<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html</a>). An object is considered immutable if its state cannot change after it is constructed (<a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html</a>).</p>
  
  <p>What I understand here is that you can only change the value of an <code>Integer</code> variable by referencing a completely different <code>Integer</code> object.</p>
  
  <p>By declaring a variable <a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4" rel="nofollow noreferrer">final</a> we can assure the following:</p>
  
  <blockquote>
    <p>Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.</p>
  </blockquote>
  
  <p>Yet again, by the <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html" rel="nofollow noreferrer">immutable</a> documentation:</p>
  
  <blockquote>
    <p>An object is considered immutable if its state cannot change after it is constructed.</p>
  </blockquote>
  
  <p>So a final, immutable <code>Integer</code>would not be allowed to change its value by any means.</p>
  
  <p>If this is correct, <strong>why aren't we allowed to declare a <code>public static final Integer</code> variable</strong>?</p>
  
  <p>The <a href="https://ideone.com/nWV4Xf" rel="nofollow noreferrer">following code</a> declares a <code>public static final Integer</code> in different ways, and all of them return a compile time error:</p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  class Ideone
  {
      public class Constants {
          public static final String STRING_CONSTANT = "string_constant";
          public static final int INTEGER_CONSTANT = 1; // allowed
          //public static final Integer INTEGER_CONSTANT = 1; // not allowed
          //public static final Integer INTEGER_CONSTANT = new Integer("1"); // not allowed
          //public static final Integer INTEGER_CONSTANT = Integer.valueOf(1); // not allowed
      }
      public static void main (String[] args) throws java.lang.Exception
      {
          System.out.println("STRING_CONSTANT = " + Constants.STRING_CONSTANT);
          System.out.println("INTEGER_CONSTANT = " + Constants.INTEGER_CONSTANT);
      }
  }
  </code></pre>
  
  <p>The exception thrown is:</p>
  
  <pre><code>Main.java:12: error: Illegal static declaration in inner class Ideone.Constants
          public static final Integer INTEGER_CONSTANT = 1;
                                      ^
    modifier 'static' is only allowed in constant variable declarations
  1 error
  </code></pre>
  
  <p>Can anyone clarify why aren't we allowed to declare a <code>public static final Integer</code>, please?</p>
  
  <p>EDIT: I'm interested in knowing why <code>public static final Integer</code> is not allowed while <code>public static final String</code> and <code>public static final int</code> are, not about finding a code that compiles.</p>