wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile time constant" Watch "compile time constant" New topic
Author

compile time constant

Tusshar Fasate
Ranch Hand

Joined: May 21, 2008
Posts: 81
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

Joined: Mar 17, 2008
Posts: 20
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.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
venu surampudi
Greenhorn

Joined: Jan 03, 2008
Posts: 16
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.


If you wanna be cocky you better be kobe !
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
priya shende
Greenhorn

Joined: Mar 17, 2008
Posts: 20
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

Joined: Mar 17, 2008
Posts: 20
Iam also not staisfied with venu's case 1 statement.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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? :/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: compile time constant
 
Similar Threads
compile time constant and final variable
compile time constant
switch statement
I dont understand switch case statement
final instance variable