Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Final, immutable objects are not constants? [duplicate]

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 Integerwould 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.

like image 947
Bernat Avatar asked Mar 29 '26 22:03

Bernat


1 Answers

The problem isn't the declaration of the constant but the fact that it's declared in an inner class that is not static. Change the declaration of the class to be static and you're good:

public static class Constants {
    public static final String STRING_CONSTANT = "string_constant";
    public static final int INTEGER_CONSTANT = 1; // allowed
    public static final Integer INTEGER_CONSTANT1 = 1;
    public static final Integer INTEGER_CONSTANT2 = new Integer("1");
    public static final Integer INTEGER_CONSTANT3 = Integer.valueOf(1);
}
like image 154
Lothar Avatar answered Apr 01 '26 06:04

Lothar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!