This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to identify compile time constants

 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I found
final int a=10;//line 1 is compile time constant
but,
final int b;//line 1
b=5; // line 2
here b is not a compile time constant
why? please explain

Thanks
Abhishek
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why? please explain


There is nothing to explain for the left hand side of the assignment, the compiler requires that the declaration be final, be either a primitive type or a string, and be initialized during the declaration.

For the right hand side -- the expression that will be assigned -- it's a bit more complex. The compiler requires that it be assigned a constant expression. If the RHS is not a constant expression, then the declared variable is not a compile time constant.

See section 15.28, for the definition of constant expression:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

Henry
 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic