• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Final Variable

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am having basic issue.

Given

final int a;
int x = 0;
a = 10;
switch(x)
{
case a:

}

Here I get compiler error at "case a :" line.

I want to know the reason.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a is not a compile time constant. The compiler cannot see that the value of a is actually 10 at compile time, because it's value could have come from a complex calculation or even something like new Random().nextInt(). The values of the cases of a switch statements must be known at compile time though. In short, it must be a numeric literal or a final variable that is initialized when declared:
The following will also not work:
That's because when you execute it one time, a can be 13, and the next time it can be 481, etc.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whether it is final or another it doesn't matter.(unless a final variable pre-initialized)
variable is not allowed as a case label, constant is required
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:a is not a compile time constant. The compiler cannot see that the value of a is actually 10 at compile time, because it's value could have come from a complex calculation or even something like new Random().nextInt(). The values of the cases of a switch statements must be known at compile time though. In short, it must be a numeric literal or a final variable that is initialized when declared:
The following will also not work:
That's because when you execute it one time, a can be 13, and the next time it can be 481, etc.



Thanks Rob
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic