• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile time constant expression

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch el daniel.
The compile constant expressions are defines here:
JLS 15.28 Constant expressions
 
el daniel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic