Chan Ag wrote:Could somebody please explain to me what a compile time constant is?
Does the above example suggest that whenever the declaration and initialization of a primitive final variable is done in a single line, we get a compile time constant?
Also, can instance variables be compile time constants?
So if I have my class as follows
are any of the instance variables( i and k ), static variable j, and Dog variable compile time constants?
Do compile time constants have to be final...
Do compile time constants have to be final if they have an explicitly declared reference ( as opposed to literals like "abc" in cases like --> g = "abc" +c; which I guess are compile time constants or aren't they?) ?
If somebody could guide me on what makes any variable a constant ( what I know is that any variable that is final is a constant as we can't change its value. Are Immutable objects constants too?
Example - cached Integer Objects from pool, String objects in String pool. Would they be constants too?) and a compile time constant, it would really help me a lot.
Thanks in advance.
Chan.
Steve
Steve
Steve Luke wrote:
Chan Ag wrote:Does the above example suggest that whenever the declaration and initialization of a primitive final variable is done in a single line, we get a compile time constant?
Also, can instance variables be compile time constants?
Yes, on both accounts. At least, in my reading of the JLS, there was no distinction between static and instance constants.
Steve
So if I have my class as follows
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
public class TestClass {
final int i;
static final int j;
final int k = 5;
Dog a;
static { j = 5;}
TestClass(){ i = 10; }
}
are any of the instance variables( i and k ), static variable j, and Dog variable compile time constants?
Given what you know now, what do you think?
Chan Ag wrote:Would that be correct?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
You know what? You need Henry Wong (one of the posters on here), because he's produced a great distillation of exactly this subject, filtered from the JLS. If I find a link to his answer, I'll post it here.
Winston
Chan Ag wrote:...
4. Dog variable a is not a constant as it is not final. So it cannot be a compile time constant.
Would that be correct?
Steve
Steve Luke wrote:
Chan Ag wrote:...
4. Dog variable a is not a constant as it is not final. So it cannot be a compile time constant.
Would that be correct?
That would all be correct, but if the variable a was declared as final Dog a = new Dog(); it would still not be a constant variable. Why is that?
Chan Ag wrote:So should I conclude that enum references are not compile time constants?
The three days can be enum elements, but their constructor sets them up as different objects with different instance fields every time that class is loaded. If “constants” have the same internal state, then those enum elements are not compile‑time constants.Jeff Verdegan wrote: . . . Logically we can consider them as such for a some purposes, . . .
Mike Simmons wrote:Well, they're not compile-time constants, certainly. The term "constant" is not really well-defined in Java, other than for a compile-time constant. And so it means different things to different people.
Chan, if you re-read the requirements for a compile-time constant, there's one very specific reason why a Dog variable can't be a constant.
A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.
Steve
Constant Names
The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech.
Campbell Ritchie wrote:
The three days can be enum elements, but their constructor sets them up as different objects with different instance fields every time that class is loaded. If “constants” have the same internal state, then those enum elements are not compile‑time constants.Jeff Verdegan wrote: . . . Logically we can consider them as such for a some purposes, . . .
One cannot ∴ say that enum elements are implicitly constants.
but imagine how much work it would take to program the compiler to identify every immutable class and recognise its objects as compile‑time constants.
Jeff Verdegan wrote:Logically we can consider them as such for a some purposes
Campbell Ritchie wrote:I think the idea behind compile‑time constants was to restrict the range of different types, in order that the compiler can be sure they are constants. I know this is a constant, and so do you… but imagine how much work it would take to program the compiler to identify every immutable class and recognise its objects as compile‑time constants.
Chan Ag wrote:
P.S : BTW that enum code is gonna help me complete my assignment. As a part of my current assignment, I'm also working on a credit card application that computes if the due date is past for payment. This enum way to have a Due date enum value seems like a wonderful idea. Thanks.
Jeff Verdegan wrote:You'd be locking in those days at the moment you launch your program...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jeff Verdegan wrote:You'd be locking in those days at the moment you launch your program...
I totally agree with what you're saying, but I could certainly see the value of a function that can return those enum constants (I think I might add PAST and FUTURE just for completeness; although you could also return null).
I didn't write it as a recommendation. I wrote it as an example where the enum constants were definitely not compile‑time constants.Jeff Verdegan wrote: . . .
I wouldn't advise using a YESTERDAY, TODAY, TOMORROW enum like the one above for that.
. . .
Campbell Ritchie wrote:
I didn't write it as a recommendation. I wrote it as an example where the enum constants were definitely not compile‑time constants.Jeff Verdegan wrote: . . .
I wouldn't advise using a YESTERDAY, TODAY, TOMORROW enum like the one above for that.
. . .