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 constant ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile time constant ?" Watch "Compile time constant ?" New topic
Author

Compile time constant ?

Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87
hi,

Is there a difference between a constant and a compile time constant in Java(correct me if i am wrong). What makes me think like this is this code.


final int i = 90;
byte b = i ;

compiles ane runs fine.


where as

final int i;
i = 90 ;
byte b = i ;

doesn't compile only.


Your opinions please. Thanks
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
I will write what I know, though even I am not sure of the source of my information.

There is no difference between a constant and a compile-time constant.

Its just that Java requires you to specify the value of a final variable in the same line that it was declared.

After that, as you know, you cannot modify it throughout the execution of the program.


With Best Regards,
Shyam Prasad Murarka
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,

Originally posted by ME:
Its just that Java requires you to specify the value of a final variable in the same line that it was declared.


The above is true only for an instance variable. This is because, instance variables are initialized by default. So, if we do not specify the value in the same line as it was declared, then it gets the default value and so, after that you cannot change it, like you tried here:


BUT, if the above was tried inside a method (so now "i" becomes a local value), then the above would be legal.

Hope I am correct.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
Originally posted by Shyam Prasad Murarka:

There is no difference between a constant and a compile-time constant

Its just that Java requires you to specify the value of a final variable in the same line that it was declared.
.


I agree that we cant change the value of the final variable once it is initialized.

But this is true that "i" here too is constant and cannot be modified.

class Test
{
public staic void main(String []arg)
{
final int i;
i = 90;
System.out.println(i);
}
}


now if i do
------------------------------------------------------------------------
THIS CODE WONT COMPILE

class Test
{
public static void main(String []arg)
{
final int i;
i = 90;
byte b = i ;// code wont compile
System.out.println(i);
}
}
--------------------------------------------------------------------------
BUT THIS WILL

class Test
{
public static void main(String []arg)
{
final int i= 90;
byte b = i ;
System.out.println(i);
}
}


If you have knowledge, let others light their candles at it.
[ April 30, 2006: Message edited by: faisal usmani ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,

Originnaly posted by faisal usmani (before that me):



Well, the above will not compile because:
  • I was trying to force an int into a byte without type casting.
  • If we change it to this:

  • byte b = (byte)i;
    then it WILL compile.

    But, this has nothing do with the topic at hand.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Shyam Prasad Murarka:
    ...Its just that Java requires you to specify the value of a final variable in the same line that it was declared...

    Not quite. For a final instance variable, you need to supply a value before the constructor exits. And while in the constructor, you cannot access that final variable until you've supplied a value.

    Here is an illustration...

    [ April 30, 2006: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Also, the original question in this thread has now been answered here, apparently since the posts in this thread didn't seem to address the actual question, instead raising loosely related side issues.
    [ April 30, 2006: Message edited by: Jim Yingst ]

    "I'm not back." - Bill Harding, Twister
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Neha Mohit:
    ...Is there a difference between a constant and a compile time constant in Java...

    Yes, according to the Java Language Specification...

    Constant Variable

    "We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable." (Ref: 4.12.4 final Variables.)


    Compile-time Constant

    One example of a "compile-time constant expression is an expression denoting a value of primitive type or a String ... composed using ... literals of primitive type and literals of type String..." (Ref: 15.28 Constant Expression.) Again, literal primitives or Strings is just one example. There are 13 others cited in the JLS.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Jim Yingst:
    Also, the original question in this thread has now been answered here, apparently since the posts in this thread didn't seem to address the actual question, instead raising loosely'related side issues.

    Doh! One minute too late.
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608
    Yes, a "constant" and a "compile-time constant" are the same thing. Both terms are used interchangeably by the specification, and there is only one type of constant.

    I still have an internal chuckle every time I see "constant variable"
    [ April 30, 2006: Message edited by: Tony Morris ]

    Tony Morris
    Java Q&A (FAQ, Trivia)
    Steve Page
    Greenhorn

    Joined: Jul 17, 2008
    Posts: 4
    I realize this is old, but I stumbled across it via Google, so...

    As briefly as possible-

    final local variables have to be initialized when they are defined.

    final instance variables can be initialized when they are defined, or they can be left unitialized and

    1. Initialized in an instance initialization block
    2. In the constructor

    Regardless, they must be initialized exactly once.

    final class variables, or constants, have the same rules as instance ones, except there is no constructor. A class initialization block looks like

    static {
    some code...
    }
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    Please let old threads rest in peace, or as we say on JavaRanch, Don't Wake The Zombies.

    This recent thread has some discussion of a similar topic.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    But Tony Morris was correct; Java officially only recognises compile-time constants.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    Yes, I think you are correct about final variables, Steve Page.
    [ July 18, 2008: Message edited by: Campbell Ritchie ]
    Rahul Shilpakar
    Ranch Hand

    Joined: Aug 29, 2006
    Posts: 132


    First, of all your mistake is that you are not converting integer value to byte value (down type casting) thats why its giving compile time error.

    Second, if you write the constant as Global variable it will not compile

    because if you write any GLOBAL final variable you must have to initialize it while declaring it (i.e. in the same line).

    Third, if you write the constant as LOCAL variable the code will compile.

    We can initialize LOCAL final variable after the declaration of the final variable.


    Perform for today. Adapt for tomorrow.
    Jd Sierra
    Greenhorn

    Joined: Jul 09, 2011
    Posts: 17

    Can anybody help me to understand following sentence posted by Steve Page?

    "final class variables, or constants, have the same rules as instance ones, except there is no constructor"
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    That bit is difficult to understand. It means that final class variables cannot be initialised in the constructor, otherwise they might be uninitialised if no instances are created, and initialised several times if several instances are created. So final class variables cannot be initialised in the contructor.
    [edit: spelling correction not→no]
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19684
        
      20

    Instead, final class variables must be either initialized immediately with the declaration, or inside a static initializer block.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Jd Sierra
    Greenhorn

    Joined: Jul 09, 2011
    Posts: 17

    Ok..got it.
    THanks Rob and Campbell
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38508
        
      23
    Well done ... and, "you're welcome."
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19684
        
      20

    You're welcome indeed
    MeghnaN Shah
    Greenhorn

    Joined: Jan 31, 2011
    Posts: 9
    the below link , helps to clarify the compile time constant properly...

    http://www.jforeach.com/compile-time-constants-in-java/116

    hope it is helpful!!!
    Shishir Choudhary
    Greenhorn

    Joined: Sep 21, 2011
    Posts: 4
    First lets go through the general rules regarding compile time constants.

    * They must be declared final
    * They are of primitive data types or String
    * They must be initialized with their declaration.
    * Their value must be constant expression.

    If you don’t understand all of this, don’t worry, we’ll go through these rules now.

    int a = 10;
    short s1 = a; //error
    final int b = 10;
    short s2 = b; //fine
    ---------------------------Why?----------------
    Now that we know what compile time constants are, we’ll now see why are they special. As the name implies, compile time constants get special treatment by the compiler.
    * They must be initialized with their declaration.
    Since the compiler knows the value of compile time constants, the compiler knows that the value of b is 10, so the compiler knows that the value of b is within the range of short data type.

    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18764
        
      40


    This is also explained in the JavaRanch FAQ...

    https://www.coderanch.com/how-to/java/JavaBeginnersFaq (item number 26)


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
     
     
    subject: Compile time constant ?