5

According to the very last note here:

"If a primitive type or a 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 a compile-time constant. If the value of the constant in the outside world changes (for example, if it is legislated 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 constant PI from within class B.

So - by the above spec, if I change the value of PI from 3.14 to, say 3.0 in class A, i have to re-compile class B to get the effect of that change in class B.

the Q here is-- what exactly is the definition of "constant" in the above spec? is it the final keyword? does any static field member "qualify" as a constant in this context? the non-static field members would be out of context here-- their values are assigned at run-time(?)

TIA.

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

EDIT:

The Q here is: what makes the compiler decide to bind the value at compile time. does the static keyword do this job all by itself. or is there anything else into it.

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

in reference to a quick answer below that keeps getting voted up:

the line on the same page:

"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 change."

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

2.) "... in combination with the final modifier": is final necessary to make the value bound in compile-time-- which i doubt it is.

2
  • I'd suggest using Math.PI instead. Commented Aug 18, 2014 at 19:57
  • +1, not for the question itself, but because TIL changing the value of static final class member can break binary compatibility! Commented Aug 18, 2014 at 20:06

4 Answers 4

3

You did 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 change.

Sign up to request clarification or add additional context in comments.

Comments

3

JLS $ 15.28. Constant Expressions

A compile-time 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).

  • Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

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

Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2) and initialization of a class or interface (§12.4.2). They may also govern the ability of a while, do, or for statement to complete normally (§14.21), and the type of a conditional operator ? : with numeric operands.

2 Comments

this - fine. however, where does this compile-time value binding take place-- wherever the constant values occur on a static context? had only in mind the field member declarations when I was asking this Q-- but now getting bigger.
Wherever the constant values happen, period.
0

what exactly is the definition of "constant" in the above spec?

It has to be final, and available for the compiler to comute using an expression without using a method.

is it the final keyword?

This is required.

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

No, making it static is an optimisation, but not required.

the non-static field members would be out of context here-- their values are assigned at run-time(?)

Not necessarily.

Comments

0
public

makes variable reachable from anywhere.

static

instantiates variable only 1 time and let you use the same address on every variable call.

final

makes the variable itself unchangeable.

That all three makes your variable Constant since it cant be changed and it only instantiates one time..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.