Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

What is Compile time constant?

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Can you give me a explantion with example regarding the "Complie Time Constant"?

Thanks
Abhijit Das
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as per my knowledge, a complie time constant is the variable whose value is defined at the same time while declaring it, and after that it can not be changed:
for example:

final int b = 5;
since we can not change the b after this line, so b is compile time constant.but
final int b;
b = 5;
this is not compile time constant(assume b is local variable) as b's value is not declared at the declaration time.

int b = 5;

this is not compile time constant because b's value can be changed after this line.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sunny.
But I have doubt , we can say that the instance variable(i.e. except local variables and except blank final variable) declares as a final modifier are compile time constants.

Then why it is given a special name?

Abhijit
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No..!!
static final variable can also come under this category..!!!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"abhijit GHY",
Please check your private messages.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Hi-Jack Removed]
[ January 31, 2008: Message edited by: Ben Souther ]
 
author
Posts: 23924
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dean Jones:
Hi all,

public static final Object t= new Object();
public final Object t1= new Object();


Are these compile time constants? Please explain.



No. The new operator is done at runtime. Hence, the object can't be determined at compile time.

Henry
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sunny Jain:
as per my knowledge, a complie time constant is the variable whose value is defined at the same time while declaring it, and after that it can not be changed:
for example:

final int b = 5;
since we can not change the b after this line, so b is compile time constant.but
final int b;
b = 5;
this is not compile time constant(assume b is local variable) as b's value is not declared at the declaration time.

int b = 5;

this is not compile time constant because b's value can be changed after this line.



Hi Sunny,
this is my query,
-b's value cannot be changed once assigned as 5(int b=5) since it is a final variable know.(correct if i am wrong)
-what is the differnce between
final int b = 5;
&
final int b;
b=5;
please explain.
note:i didn't understand this compile time & run time constant phenomenon.

prabhu

prabhu
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends this is Dattaraya.......:>
Dear Friend Sunny!
If we have declared instance variable as final & we don't initialise it at declaration time then it will be initilised to its default value of data type, after that we can't reinitialise that instance variable,instance variable is always be compile time constant if it declared as final.

___________________________________________________________________________

This is the correction of your statement of " instance variables are come under the categories of compile time constant & we can do like this to instance var i.e. final int b;
b=5;"
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Hi-Jack Removed]

Dean,
I you have a question, please start your own thread.
[ January 31, 2008: Message edited by: Ben Souther ]
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic