• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compile time constants & Final variable

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can anyone please tell me what are compile time constants?
1>final int a=10; //Is this a compile time constant?
2>int b=0;//Is this a compile time constant?
3>Is it like,all variables that are marked final are compile time constants?

Thanks.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Yes you got it right putti, Variables that are marked as final are called as Compile Time Constants. Thease are constants because once assigned their value cannot be changed.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandy ,

I don't agree with

Variables that are marked as final are called as Compile Time Constants.



Look at :


i is final but here not a compile time constant.

"Compile time constants" are defined :
here
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait! !! Can you guys tell me the difference between "a constant" and "compile-time constant" please?
Coming to final variables, they say values ONCE INITIALIZED can not be changed. So you are safe until you have not assigned any value. And hence Seb's code...


Compiles fine. But if I place another statement...

i=2;

Right below the line 1, the compiler alarms... with,

variable i might already have been assigned
i=2;
^
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Akhil,

I just say that final variables are not always compile-time constants.

In my example above, i is a final variable, which must must be definitely assigned at the end of every constructor of the class in which it is declared

In the expression
static final int j= 2*3;

The compile-time constant is 2*3, which is assigned to j.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seb, I got the difference between constants and compile-time constants. Yes, I do support your point that final variables may be compile time constants and that doesnt mean " every variable that is declared final is a compile time constant.
[ October 05, 2005: Message edited by: Akhil Trivedi ]
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting seb
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent work SEB!!!
thanx!!!
a great info !!!
thank u
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably the best solution to this kind of problem is to first find the definition of compile-time constant, and then discuss it.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree Barry, the link was in my first post...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic