File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch Statement

 
Sridhar Padala
Greenhorn
Posts: 20
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 3742
16
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very much Muthu......
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic