• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to identify compile time constants

 
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
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!