Compile-value binding values ​​for Java constants

According to the last entry here :

"If a primitive type or string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called the compile time constant. The value of the constant in the outside world changes (for example, if it is assumed that pi actually should be 3.975), you will need to recompile any classes that use this constant to get the current value.

Suppose I defined a public constant PI ( public static final double PI=3.14 ) in class A , and used this PI constant from class B

So - according to the above specification, if I change the PI value from 3.14 to, say, 3.0 in class A , I need to recompile class B to get the effect of this change in class B ,

here Q - what is the definition of "constant" in the above specification? is this the final keyword? does any member of the static field qualify as a constant in this context? non-static field members will be out of context here - their values ​​are assigned at runtime (?)

TIA.

// =============================

EDIT:

Here's Q: what the compiler does is decide to bind the value at compile time. The static does this work on its own. or is there something else in it.

// =========================

in connection with a quick reply below that continues to vote:

line on the same page:

"The static modifier, in combination with the final modifier, is also used to define constants. The last modifier indicates that the value of this field cannot be changed."

1.) "... is also used to define constants ....": what else defines a constant.

2.) "... combined with the final modifier": final necessary to make the value attached at compile time - which I doubt it.

+5
source share
4 answers

You have not even read the link you mentioned?

Constants

The static modifier in combination with the final modifier is also used to define constants. The final modifier indicates that the value of this field cannot be changed.

+3
source

JLS $ 15.28. Constant expressions

A compile-time constant expression is an expression representing a primitive type or String value that does not end abruptly and is composed using only the following:

  • Primitive type literals and String type literals (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)

  • Passes primitive types and String casts (§15.16)

  • Unary operators +, -, ~, and! (but not ++ or -) (§15.15.3, §15.15.4, §15.15.5, §15.15.6)

  • Multiplicative operators *, / and% (§15.17)

  • Additive operators + and - (§15.18)

  • The shift operators <, → and →> (§15.19)

  • Relational operators <, lt; =,>, 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 operator or operator || (§15.23, §15.24)

  • Ternary conditional operator ?: (§15.25)

  • Generalized expressions (§15.8.5), the containing expression of which 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 TypeName form. An identifier that refers to constant variables (§4.12.4).

  • String compile-time constant expressions are always interned to exchange unique instances using the String.intern method.

  • A constant compile-time expression is always considered FP-strict (§15.4), even if it occurs in a context where a non-constant expression is not considered FP-strict.

Compile-time constant values ​​are used in case methods in switch statements (§14.11) and are of particular importance for assignment conversion (§5.2) and initialization of a class or interface (§12.4.2). They can also control the ability of a while, do, or for approval for normal termination (§14.21) and the type of conditional operator ?: with numeric operands.

+3
source

What is the definition of "constant" in the above specification?

It must be final and available to the compiler for commenting using an expression without using a method.

Is this the last keyword?

It is required.

does any member of the static field qualify as a constant in this context?

No, static is an optimization, but not required.

non-static field members will be out of context here - their values ​​are assigned at runtime (?)

Not necessary.

0
source
 public 

makes the variable accessible from anywhere.

 static 

creates a variable only 1 time and allows you to use the same address with every call to the variable.

 final 

makes the variable itself immutable.

That all three make your Constant variable, because it cannot be changed, and it is created only once ...

0
source

Source: https://habr.com/ru/post/1200619/


All Articles