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.