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 compile time constant expression 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 "compile time constant expression" Watch "compile time constant expression" New topic
Author

compile time constant expression

el daniel
Greenhorn

Joined: Nov 15, 2002
Posts: 6
I've got a problem with compile time constant expression.
There are several places in which we need them, for example they are used in case labels in switch statements....
first consider this example:

According to JLS rules ,(n==2) is not
considered to be a compile time constant expression, although "n" is known @ compile time. hence a compile time error occurs.
To fix the problem, we can do this,for example:

the if() statement is now a compile time constant expression.
But WHY the example below doesn't work?

[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 31, 2003: Message edited by: Jessica Sant ]
Ambapali Pal
Ranch Hand

Joined: Dec 17, 2002
Posts: 47

class CompilTimeConst{
public static void main(String[]arg){
int k;
int j=2;
final int n = j;
if (n == 2)
k = 3;
// k is not "definitely assigned"
System.out.println(k);
}}


In this case n is blank final variable which is not a compile time constant. n will actually get the value at the run time depending upon whatever the value of the j.
But if you try this way it will compile.

class CompilTimeConst{
public static void main(String[]arg){
int k;
final int j=2;
final int n = j;
if (n == 2)
k = 3;

System.out.println(k);
}}

Also one more point about the the local final variable is that you can keep it blank if you do not use it. Just like othet local variable.
Hope it helps a bit.
Ambapali
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch el daniel.
The compile constant expressions are defines here:
JLS 15.28 Constant expressions


SCJP2. Please Indent your code using UBB Code
el daniel
Greenhorn

Joined: Nov 15, 2002
Posts: 6
thanks,
if i understand well, the value of a local and class member variable aVar ( consider int aVar=1 ) is known @runtime and not @compile time?
thank you
[ January 31, 2003: Message edited by: el daniel ]
[ January 31, 2003: Message edited by: el daniel ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Do you mean a declaration like this?
int aVar=1;
The compiler knows it very well. The compiler knows the type of both the variable and the value to be assigned to. The compiler checks the rules for assigment conversion. The compiler also knows that aVar is not a compile constant expression (JLS 15.28).
In this example.
Object o = aPossibleParentClass;
The compiler checks again the rules for assignment conversion. But only knows that the declared type of "o" is Object. It cannot know the real type (runtime type) of the instance being assigned, because it could be a subclass of PossibleParentClass
[ February 01, 2003: Message edited by: Jose Botella ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constant expression