Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Switch Statement

 
Sridhar Padala
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to compile the below program
class Switch1 {

public static void main(String args[])
{
final int a = 1;
final int b;
b= 2;
int x = 1;

switch (x) {
case a:
case b:
}}}

Error:
Switch1.java:13: constant expression required
case b: //
^
1 error

I want to know what is the difference between declarations mentioned below

final int a = 1;

and

final int b;
b= 2;

Thanks in Advance :-)
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sridhar Padala ,

Welcome to Javaranch.

Actually the switch statement expects its all 'case' expressions to be final and should get resolved at compile time. Thats the reason you get a compiler error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Jim Yingst's post in this thread for further information.
[ August 14, 2007: Message edited by: Joanne Neal ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that link has the perfect and exact answer what you might look at.

Thanks for pointing to the link Joanne Neal.
 
Sridhar Padala
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very much Muthu......
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!