Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time constant

 
Tusshar Fasate
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does actually compile time constant mean?

final int a=5;//compile time constant

final int a;//not compile time constant
a=5;

Is above example true?
Can anyone elaborate cocept of compile time constant?
 
priya shende
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tushar,
I am also just studying for SCJP.So dont know much.But I will try to explain.

The word constant has a special meaning in Java. It doesn�t just mean �a value that doesn�t change;� a better word for that is immutable. In Java, the word constant �or more precisely, the phrase constant expression�means �a value that is computed at compile-time.�

In Java, a constant value is always computed at compile time

"compile-time constant expression":
A compile time constant expression is an expression whose value can be determined at compile time (during verification), before any part of the program has been executed.

In the case of a compile-time constant the compiler is allowed to �fold� the constant value into any calculations in which it�s used; that is, the calculation can be performed at compile time, eliminating some run-time overhead. In Java, these sorts of constants must be primitives and are expressed using the final keyword. A value must be given at the time of definition of such a constant.

The second statement is creating a Blank Final i.e which are fields that are declared as final but are not given an initialization value. In all cases, the blank final must be initialized before it is used, and the compiler ensures this. However, blank finals provide much more flexibility in the use of the final.


final int a=5;//compile time constant
Here a is initialized final.


final int a;//not compile time constant
Here a is a blank final.
a=5;



Priya Shende
-------------------
Studying for SCJP.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explaination priya, but i have a following doubt,
If i declare the following as an instance variable :
final int a;
then inside some method, if i try to assign it some value:
suppose
a= 5;

I think compiler should not allowed this, becuase compiler initialized every instance variable by default value,so before the line(a =5) is executed, a will be having initial value of 0. so it should be an error.
 
venu surampudi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the behaviour i noticed.

Case 1
If a variable is declared as an instance variable, and not assigned a value, it will be assigned a default value and CANNOT be modified any where in the code, the variable can however be assigned a value in the declaration statement.

final int x; // will be set to 0 and is final and cannot be modified

fianl int x = 5; // will be set to 5 and cannot be modified.

however just declaring x as final, not initializing it and trying to intitialize its value in a method will cause an error.

case 2:

Now if the variable is declared as final inside of a method, and not initialized, you get one and only shot to assign it.

I think case 1 is compile time constant and case 2 is not.

Let me know if i am lying.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by venu surampudi:

Case 1
If a variable is declared as an instance variable, and not assigned a value, it will be assigned a default value and CANNOT be modified any where in the code, the variable can however be assigned a value in the declaration statement.


Not entirely true.
You can delay a final variable's initialization until the object is constructed.

 
priya shende
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunny,
Final instance variables can be initialized at their declaration.
If they are not initialized in their declarations, they must be intialized in constructors.
---------------------------------------

class Hello{
final int a;
Hello(){
a=5;
}

void show(){
System.out.println("Final instance variable"+a);
}

public static void main(String ar[]){
Hello h= new Hello();
h.show();
}
}
-----------------------------------------

priya...
-------------
studying for scjp.
 
priya shende
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam also not staisfied with venu's case 1 statement.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priya shende:
Hi Sunny,
Final instance variables can be initialized at their declaration.
If they are not initialized in their declarations, they must be intialized in constructors.
---------------------------------------

class Hello{
final int a;
Hello(){
a=5;
}

void show(){
System.out.println("Final instance variable"+a);
}

public static void main(String ar[]){
Hello h= new Hello();
h.show();
}
}
-----------------------------------------

priya...
-------------
studying for scjp.



How is this different from what I said an hour earlier? :/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic