• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

final in switch case

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test
{
public static void main(String[] args)
{
final int a = 1;
final int b;
b = 2;
int x = 0;
switch (x) {
case a: // ok
case b: // compiler error
}
}
}
Please explain me why the above code is giving error
 
lowercase baba
Posts: 12754
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generally speaking, it is of TREMENDOUS help if you post the actual text of the error message. without it, we either have to guess, or copy your code and compile it ourselves. The easier you make it for others to help you, the more likely you are to get help.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, use code tags



after this line of code the value of b can never be changed,
it is therefore undefined

what's the next line of code?
[ January 24, 2008: Message edited by: Bill Shirley ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bill Shirley:
also, use code tags



after this line of code the value of b can never be changed,
it is therefore undefined

what's the next line of code?

[ January 24, 2008: Message edited by: Bill Shirley ]



Not entirely true.
You can delay the inital assignment of a final variable.

Example:
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Values we use in switch cases must be "COMPILE TIME CONSTANT"

In your case a is defined at the compile time, but for b it is initialize at the run time..!!

Since both a and b are local Variable, following checks are done by compiler :

1) Variable are initialize before using
2) In case of final variable, see after initialization there should not be any probability of changing their value..!!

In this case compiler checks that Variable b has been given a value, but that value will be assigned at run time..so it is giving an error because it wants value at the compile time...!!
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using anything except literals or "static final" constants as switch cases is very bad form. So, while you do seem to have found a situation where the compiler is being a bit dimmer than one might hope, its real-world relevance is very slight indeed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!