wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Swtich Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Swtich Statement" Watch "Swtich Statement" New topic
Author

Swtich Statement

Polishetty Raghavendra
Greenhorn

Joined: Apr 09, 2006
Posts: 18
What is the difference between these two programs ?
1 )

public class switch1 {
public static void main(String[] args){
final int a = 1;
final int b = 2;
int x = 0;
switch (x) {
case a: // ok
case b:
}
}
}

2 ) public class switch1 {
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:
}
}
}

Could some one explain it ?


Regards,<br />P Raghavendra<br />SCJP 1.5 - 70%
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707



case constant must be compile time constant. b has not been assigned the
value where it is declared hence using final variable b in the case
causes compiler error.


Thanks,


cmbhatt
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
The switch build the "if" cases structure based on compilation time (for example you cannot change a or b on runtime).
In your second code you have a compilation error because the b variable is not initialized at the compilation time. The compiler cannot know that you will initialize it later.

Ciao
Polishetty Raghavendra
Greenhorn

Joined: Apr 09, 2006
Posts: 18
Chandra,

Before using the constant it has assigned with the value 2.

Regards
Raghavendra
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Raghavendra,


The case constant must be compile time constant, because the case
argument has to be resolved at compile time.
The variable that has to be
used in the case must be assigned a literal at the time it is
declared.

final int b;
a=10; //b is not compile time constant because it is assigned the value
at run time.


Thanks,
Polishetty Raghavendra
Greenhorn

Joined: Apr 09, 2006
Posts: 18
Thanks Chandra,

I got it now.

Regards
Raghavendra
sumi selva
Greenhorn

Joined: Jul 10, 2007
Posts: 19
Hi,

I tried the following code
class Test {
static int x ; ---> line 1
x = 3;
public static void main(String[] args) {

}
}

even if I substitute int x (or) final int x (or) final static int x
I get a compile error.

Should a instance variable always be a compile time constant.cant we assign them values at runtime.

Thanks,
Sumi
Yeming Hu
Ranch Hand

Joined: May 14, 2007
Posts: 37
Originally posted by sumi selva:
Hi,

I tried the following code
class Test {
static int x ; ---> line 1
x = 3;
public static void main(String[] args) {

}
}

even if I substitute int x (or) final int x (or) final static int x
I get a compile error.

Should a instance variable always be a compile time constant.cant we assign them values at runtime.

Thanks,
Sumi


x=3 should be in a method or initialization block.


Best Wishes,<br /> Yeming
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swtich Statement
 
Similar Threads
switch statement problem
switch stmt
final in switch case
Switch Statement
switch & case