wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic difference" Watch "Basic difference" New topic
Author

Basic difference

Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
I know this may be basic to many but could somebody explain the difference between compile-time constants and run-time constants.
I am guessing final and static variables are comile time variables. Am I right?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification 15.28.

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.5)
* Casts to primitive types and casts to type String
* The unary operators +, -, ~, and ! (but not ++ or --)
* The multiplicative operators *, /, and %
* The additive operators + and -
* The shift operators <<, >>, and >>>
* The relational operators <, <=, >, and >= (but not instanceof)
* The equality operators == and !=
* The bitwise and logical operators &, ^, and |
* The conditional-and operator && and the conditional-or operator ||
* The ternary conditional operator ? :
* Parenthesized expressions whose contained expression is a constant expression.
* Simple names that refer to constant variables (�4.12.4).
* Qualified names of the form TypeName . Identifier that refer to constant variables (�4.12.4).
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It might make sense for a compile-time constant to be static, but that's not a requirement (except in an interface).

"Constant variable" is defined under JLS - 4.12.4 final Variables: "...a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable." The key here is the definition of "compile-time constant expression," which is the section Keith quoted above.

I don't think the term "runtime constant" is formally defined. But I suppose conceptually...

final int i = (int)(Math.random() * 10);
[ November 08, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
 
subject: Basic difference
 
Similar Threads
difference between final variable in JAVA and constant in C
Static Final Variables Query
compile time constants & final var
static variable in a servlet
Final Variable Behavior