File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes compile time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time constants" Watch "compile time constants" New topic
Author

compile time constants

Ravi Bansal
Ranch Hand

Joined: Aug 18, 2008
Posts: 84
Can anyone tell me what is compile time constant.

if i declare a final variable like this

final int i=5;

This is a compile time constant

and if i declare like this
final int i;
i=5;

this is not a compile time constant

I want to understand what is a difference between above two intialization of final variables.


SCJP 5.0 94%
OCE-EJBD 90%
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11152
    
  16

The compiler is dumb. It has a hard time connecting things across multiple lines. Consider that legally, there could be 800 lines of code between those two lines you posted in your second example.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
The official definition of a "constant" is in the Java Language Specification (I think, here).

Do a search through this forum for "compile time constant" because that sort of question comes up regularly here. Some people say "runtime constant" but that does not appear to be an official Java term. You should find out about that from your search as well. Good luck with it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constants
 
Similar Threads
Why this error Incompatible types?
switch statement and compile time constatns.
compile time constant
compile time constant
complie time constants