• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is b = 2 not a compile-time constant?  RSS feed

 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: To be a variable that is a compile time constant, the variable needs to be...

  • declared as final
  • have a primative or String type
  • initialized (on the same line as the declaration)
  • assigned to a compile time constant expression


  • I'm just learning the details of the switch statement and since the case constant must be a compile time constant that brought me back to your article ;)

    b is NOT a compile constant because - unlike a - it was not initialized on the same line as the declaration...
    Now I have 2 questions: maybe it's just because english is not my native language... but as far as I understand the term initialization (also as far as Java is concerned) means to assign a variable a value for the very first time - and that does NOT have to be on the same line as the declaration of the variable... is that correct? And if that is correct... then where exactly in the JLS did you figure out that in order for a final variable to count as a CTC (compile time constant) it has to be initialized on the same line as its declaration? Or does the term initialization in Java really mean assignment of a value in the same line as the declaration?

    Many thanks!
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem is trying to program a compiler which can go through all paths of execution and confirm that the variable is awlays assigned the same value. Try writing a compiler which can distinguish this:-and this:-Both are valid, but it would be very difficult to program a compiler to recognise both. That is why Java uses that particular definition of a constant expression. Athough 1, 2 and 999 are constant expressions, neither i nor j is a constant expression.

    The quotation of Henry Wong's comes from this thread.
     
    Vince Valentin
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A declaration declares the variable. It tells the compiler that you need some memory set aside to hold a value at some point.

    An assignment actually puts a value into the memory set aside for that variable:

    So having your declaration and your assignment on the same line...
     
    Bora Sabrioglu
    Ranch Hand
    Posts: 100
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    now I understand..
    It this case it's pretty difficult for the compiler to figure out which value will be assigned to j... that's why j is not a CONSTANT at COMPILE TIME (even if its final).
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    BTW, in terms of the answer, Campbell's example is perfect at explaining why it can't be a compile time constant... have a cow.

    In terms of the Java Language Specification, it isn't all that clear. In my opinion, the JLS is a bit vague in this regard. In the section of final variables, it purposely avoids using the word initialized -- instead using the words assigned and unassigned, even though assignment can only occurs once. In the section regarding constant variables, it does use the word initialize, leading to the conclusion that initialize is assignment during declaration. Or more correctly, leading to the conclusion that they are different, and the actual implementation leading to the conclusion.

    Does initialize mean assignment at declaration? Very likely. Is it defined anywhere in the JLS? Not sure. I certainly haven't found it. Perhaps someone can wade through the mess and find some reference to it.

    Henry
     
    Campbell Ritchie
    Marshal
    Posts: 56533
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not convinced that initialize is used in the JLS at all. Initializer is.

    I don't feel I deserve a cow; I was only doing my job, but thank you
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!