aspose file tools*
The moose likes Java in General and the fly likes Switch Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Switch Statement" Watch "Switch Statement" New topic
Author

Switch Statement

Sridhar Padala
Greenhorn

Joined: Aug 03, 2007
Posts: 20
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

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
See Jim Yingst's post in this thread for further information.
[ August 14, 2007: Message edited by: Joanne Neal ]

Joanne
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Aug 03, 2007
Posts: 20
Thank You Very much Muthu......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Statement
 
Similar Threads
I dont understand switch case statement
Compile Time constant??
Doubt about Switch statement (Compile Time Constant)
Legal expressions for switch and case statements
Swtich Statement