aspose file tools*
The moose likes Beginning Java and the fly likes What is a compile time constant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a compile time constant? " Watch "What is a compile time constant? " New topic
Author

What is a compile time constant?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

It is amazing how many times a question comes up in this forum, where the answer is something like... "Because X is a compile time constant, the compiler will Y"... But what exactly is a compile time constant? And how can we determine whether something is treated as such?

Obviously, a compile time constant is a constant value that is known at compile time... ... Literals are, by definition, compile time constants -- as they are constants, known at compile time.

But the definition of a compile time constant is a bit more complex. To start, let's examine section 15.28 of the Java language specification.

15.28 Constant Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:

  • Literals of primitive type and literals of type String
  • 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 ? :
  • Simple names that refer to final variables whose initializers are constant expressions
  • Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions



  • This is the full definition of a compile time constant. And as you can see, it contains more than just literals. In fact, literals are merely the first bullet point on the list. Also, note that a compile time constant can apply to any literal that is of primative or String type.

    The next few bullet points are the operations that can be applied to a constant at compile time. This list is actually pretty long, as it is possible to apply most of the operations at compile time. It may actually be easier to remember what can't be apply at compile time -- pre and post increment and decrement, instanceof operator, or any method calls, are not on the list.

    The last few bullets are the most interesting. It is possible to use a variable in the expression -- provided that the variable is a compile time constant variable. So... what is a constant variable? Going back to the JLS (section 4.12.4 to be exact)...

    4.12.4 final Variables

    A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

    [BLAH BLAH BLAH]

    We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).


    The last part of the definition is the relevant part (I still find it amazing that this is that well hidden in the specification). 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





  • Obviously, I am somewhat bored this morning... ...But hopefully... now when someone say, "what's a compile time constant", or "because X is a compile time constant", you know the long specification definition of what it is.

    Hope this helps,
    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Cameron Wallace McKenzie
    author and cow tipper
    Saloon Keeper

    Joined: Aug 26, 2006
    Posts: 4968
        
        1

    This is an awesome, and definitely helpful resource.

    Just in case anyone was looking for it, here's a link to the Java Language Specification from Sun which was referenced by Henry:

    Java Language Specification from Sun

    -Cameron McKenzie
    Shivani Yadav
    Greenhorn

    Joined: Aug 19, 2009
    Posts: 6
    This is a good detailed information..which i was looking for. Last four points related to "To be a variable that is a compile time constant, the variable needs to be... " are very helpful.
    Thanks


    ♫ The deepest secret...life is not a process of discovery, but a process of creation. Seek therefore, not to find out Who You Are, but seek to determine Who You Want to Be
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19066
        
      40

    ash aj wrote:This is a good detailed information..which i was looking for. Last four points related to "To be a variable that is a compile time constant, the variable needs to be... " are very helpful.
    Thanks


    Thanks for the feedback. I'm grateful. In thinking about it a bit more, maybe it could be tighten up. Instead of this...

  • initialized (on the same line as the declaration)


  • Maybe this would read better...

  • initialized (at the same time as the declaration)


  • Henry

    Raghu Devatha
    Ranch Hand

    Joined: Feb 10, 2009
    Posts: 39
    I am sorry, new to Java Ranch, do we store these kind of articles elsewhere or this thread will be the only source, because this is really helpful.


    dR
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19066
        
      40

    Raghu Devatha wrote:I am sorry, new to Java Ranch, do we store these kind of articles elsewhere or this thread will be the only source, cuz this is really helpful.


    This topic was added to the FAQ...

    http://faq.javaranch.com/java/JavaBeginnersFaq


    Henry
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    You will have to learn your way around the API documentation, Java Language Specification, etc. You may prefer to keep your own notes. And this thread will most probably remain available as long as JavaRanch lasts.

    Please don't say "cuz": read this FAQ
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    Bora Sabrioglu,
    Your post was moved to a new topic.
    (This informational message will self destruct in two days)


    [NOTE: Normally, this post will "self destruct". However, since the post that was moved has an interesting discussion, I think it would be good to keep this link.]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: What is a compile time constant?