Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any formal definition of constants?

Tags:

java

jls

Does the Java specification define the principle of constants, or is it left to a recommendation role?

If it is defined in the specifications, what is its definition?

Concretely, are any or all of the following examples considered as constants? If some or all indeed are, are they considered so following the specification, or any other official recommendation?

public static final int ONE = 1;
public static final double TWO = 2.0d;
public static final String THREE = "three";
public static final ImmutableList<Integer> ONE_TWO_THREE = ImmutableList.of(1, 2, 3);
public static final Logger logger = LogManager.getLogManager().getLogger(ThisClass.class);
like image 341
Olivier Grégoire Avatar asked Apr 04 '17 12:04

Olivier Grégoire


1 Answers

There are two uses of constant in the Java language. There are constant expressions and those are defined in the specification. See Chapter 15.28 Constant Expressions

A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)
  • Casts to primitive types and casts to type String (§15.16)
  • The unary operators +, -, ~, and ! (but not ++ or --) (§15.15.3, §15.15.4, §15.15.5, §15.15.6)
  • The multiplicative operators *, /, and % (§15.17)
  • The additive operators + and - (§15.18)
  • The shift operators <<, >>, and >>> (§15.19)
  • The relational operators <, <=, >, and >= (but not instanceof) (§15.20)
  • The equality operators == and != (§15.21)
  • The bitwise and logical operators &, ^, and | (§15.22)
  • The conditional-and operator && and the conditional-or operator || (§15.23, §15.24)
  • The ternary conditional operator ? : (§15.25)
  • Parenthesized expressions (§15.8.5) whose contained expression is a constant expression.
  • Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).
  • Qualified names (§6.5.6.2) of the form TypeName . Identifier that refer to constant variables (§4.12.4).

If you follow the link for constant variables, you'll find

A blank final is a final variable whose declaration lacks an initializer.

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9), and definite assignment (§16 (Definite Assignment)).

So static is not required. The Java language only cares that the variable is final and initialized at its declaration with a constant expression.

There are also enum constants which are the enum instances.


Other uses are developer uses to refer to something that doesn't change (whether it's a non-constant final variable or something else). Careful how you use those in conjunction with the constants above.


That being said, your examples contain variables, which are considered constants according to the above mentioned specification as well as those, which are not. The first three variables are final and of primitive type or of type String and are, thus, constant variables:

public static final int ONE = 1;
public static final double TWO = 2.0d;
public static final String THREE = "three";

Although being declared final, the last two variables are not to be considered constant variables, because they are neither of primitve type nor of type String:

public static final ImmutableList<Integer> ONE_TWO_THREE = ImmutableList.of(1, 2, 3);
public static final Logger logger = LogManager.getLogManager().getLogger(ThisClass.class);
like image 158
3 revs, 2 users 87% Avatar answered Sep 22 '22 03:09

3 revs, 2 users 87%