File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes complie time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "complie time constants" Watch "complie time constants" New topic
Author

complie time constants

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
What I understand is Compile time constants are variables declared as final and initialized at the time of definition like

final int a =10; ---> this is a compile time constant.Right?

But what about this code:

final int a;
a=10;

In this case is 'a' not a compile time constant.
Please explain.

Thanks
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello,

Let's see these three cases:

<blockquote>code:
<pre name="code" class="core">
final int anInt = 10;
short aShort = anInt;
</pre>
</blockquote>

In this case will compile ok, because constants (final int) initialized this ways can be Guaranteed it is initialzed within the range of a short.

<blockquote>code:
<pre name="code" class="core">
final int anInt;
anInt = 10;
short aShort = anInt;
</pre>
</blockquote>

but, in this case, we will have a compile-time error, because its not guaranteed the range of the int will be a short. It's the same of this one:

<blockquote>code:
<pre name="code" class="core">
int anotherInt = 20;
final int anInt = anotherInt;
short aShort = anInt;
</pre>
</blockquote>

anotherInt could be a variable that came from a GUI, for example, and it can have a range higher then a int.

Yours,
Raphael Rabadan


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hello,

This is going over my head.This is not clear to me at all.After all value 10 means 10 isn't it? How does it matters in which way it is assigned to a variable ie:

final int a =10;

or

final int a;
a=10;
kamal shah
Greenhorn

Joined: Feb 26, 2008
Posts: 18
Hi,

See compiler dose not have beautiful mind like us to think or assume.it works based on some rules.

Final variable is not suppose to change through out your program,to ensure this compiler want you to assign a fixed value to this variable at the time of declaration.

#Case 1:
final int a=10;

#Case 2:
final int a;
a=10;

if Case 2 would have allowed by the compiler than final variable can be reassigned any time you want in your program.Compiler has no way to ensure the functionality of final variable other than compelling you to assign it a value at the time of declaration.

Value assigned in both the case is same i.e. 10 but the way you do really matters.


Regards,<br />Kamal<br /> <br />Life is nothing but a competition to be the criminal rather than the victim -Bertrand Russell
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi,

So does this means that declaring a final variable after defining it means that it is no longer a final variable.
What the difference between the 2 types of assignment?
balakrishnan srinivasan
Greenhorn

Joined: Jul 20, 2008
Posts: 1
As far as I know, you will not be able to compile a code which has something like this( irrespective of final or non-final specifiers.)
int a;
a=10;

Final variables can be assigned a value in 2 ways 1) at the time of declaration like final int anInt = 10; 2) Or within the constructor of the class where you have the variable(I think from JDK1.4 onwards).
E.g. Class FinalCheck{
final int anInt;
FinalCheck(){
anInt = 10;
}
}

Hope this answers your question.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Given, say local variables in main method,

final int i = 0;
final int j;
j = 1;

both, i and j, are constants. If you are going to change the value of one later on, you'll get a compiler error. So what's the difference between them ?

1. i is a compile time constant. That means, every time, i occurs in your code, the compiler substitutes the variable i through its value in the bytecode. Therefore the bytecodes of

final int i = 0;
byte b = i;

and

final int i = 0;
byte b = 0;

are quasi identical.

2. After assuring that the value of j, once assigned, doesn't change, j is treated by the compiler like a usual variable, those value he doesn't know. Therefore the bytecodes of

final int j;
j = 1;
int k = j;

and

int j;
j = 1;
int k = j;

are quasi identical.

You can check the statements about the bytecode by using the decompiler javap.

Hope this clarifies the situation a little bit.
[ July 20, 2008: Message edited by: Ralph Jaus ]

SCJP 5 (98%) - SCBCD 5 (98%)
Naveen Kumar G
Greenhorn

Joined: Jul 14, 2008
Posts: 10
Hi Balakrishnan,
As far as I know, you will not be able to compile a code which has something like this( irrespective of final or non-final specifiers.)
int a;
a=10;


I dont think there is any problem with this piece of code. It will be treated just like a normal variable. It wont result in a compiler error.

As for as I know, only when we try to implicitly do a narrowing conversion on a variable(or anything which is NOT a compile time constant), it will result in a compiler error.

Please let me know if i'm wrong..

Ralph Juas,
Your explanation was really nice.. thanks for that..
[ July 21, 2008: Message edited by: Naveen Kumar G ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complie time constants