• 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

switch case with final

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In the following code
public static void main(String args[]){
final int a=1;
final int b;
b=20;//is this b not final
int x=0;
switch(x){
case a:
case b: //compiler error


}
}
I want clarification as to why is b not considered to be a compile time constant???.

Thanks
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a constant because it can be changed. The compiler does not know whether or not you will change it, but it knows that it might be changed because it is not final.
 
kalpana Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
Thanks a lot for the response.
But in the above code b is defined to be final. it is initialised in the next line.
I wanted to know if there is any difference in this type of initialization.

Thanks
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kalpana Kumar:
final int a=1;
final int b;
b=20;//is this b not final



Both a and b are final, but a is declared with an initializer
that is a constant expression. b is declared as blank final.

Originally posted by kalpana Kumar:
But in the above code b is defined to be final. it is initialised in the next line.
I wanted to know if there is any difference in this type of initialization.



Yes there is. Here are two pointers into the JLS you can
take a look at:

constant expressions (see the last two items in the bulleted list)

definite assignment of blank finals
[ November 06, 2005: Message edited by: Brian Cole ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have confused a constant with a final.
A final is not always a constant, but a constant is always final.
Your issue is related to constants, not finals.
This is why you observe some finals, that are not constants, behaving differently to other finals, that are constants.

The links above are your point of call (JLS 15.28 and JLS 16).
Here is some fun: http://jqa.tmorris.net/trivia/GetQAndA.action?qids=13
 
kalpana Kumar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for all the replies.
 
reply
    Bookmark Topic Watch Topic
  • New Topic