Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile-time value-binding of Java constants

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.

like image 857
user3880721 Avatar asked Aug 18 '14 19:08

user3880721


People also ask

What are compile-time constants in Java?

4.1. Compile-Time Constants. A Java variable is a compile-time constant if it's of a primitive type or String, declared final, initialized within its declaration, and with a constant expression. Strings are a special case on top of the primitive types because they are immutable and live in a String pool.

Is constant compile-time?

A compile-time constant is a value that can be (and is) computed at compile-time. A runtime constant is a value that is computed only while the program is running. If you run the same program more than once: A compile-time constant will have the same value each time the application is run.

Are enums compile-time constants?

Enum values or Enum instances are public, static, and final in Java. They are a compile-time constant, which means you can not changes values of Enum instances and further assignment will result in a compile-time error.

What is compile-time constant in Dart?

Dart has the concept of compile-time constants. A compile-time constant is parsed and created at compile time, and canonicalized. For example, here is a const constructor for Point: class Point { final num x, y; const Point(this.x, this.y); }


Video Answer


2 Answers

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.

like image 59
Smutje Avatar answered Nov 15 '22 01:11

Smutje


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.

like image 33
Jeroen Vannevel Avatar answered Nov 15 '22 01:11

Jeroen Vannevel