The class Integer
is a wrapper of the int
primitive type (https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html). An object is considered immutable if its state cannot change after it is constructed (https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html).
What I understand here is that you can only change the value of an Integer
variable by referencing a completely different Integer
object.
By declaring a variable final we can assure the following:
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.
Yet again, by the immutable documentation:
An object is considered immutable if its state cannot change after it is constructed.
So a final, immutable Integer
would not be allowed to change its value by any means.
If this is correct, why aren't we allowed to declare a public static final Integer
variable?
The following code declares a public static final Integer
in different ways, and all of them return a compile time error:
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);
}
}
The exception thrown is:
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
Can anyone clarify why aren't we allowed to declare a public static final Integer
, please?
EDIT: I'm interested in knowing why public static final Integer
is not allowed while public static final String
and public static final int
are, not about finding a code that compiles.
You can find the reason behind this in the JLS.
8.1.3. Inner Classes and Enclosing Instances
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).
Then, we can check the definition of a constant variable :
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression
This is why you are allowed to declare a primitive or a String
constant. But the Integer
class and other boxing class are not part of that exception, those are instance like any other class.
Source : Andy Thomas
Inline constant
If we add that with the following :
A reference to a field that is a constant variable (§4.12.4) must be resolved at compile time to the value V denoted by the constant variable's initializer.
We can see that those constant don't really exist at run time, the references are resolved at compile time.
Code :
final static int INTEGER_CONSTANT = 1;
int value = INTEGER_CONSTANT;
Run time "code" :
int value = 1;