Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swtich Statement

 
Polishetty Raghavendra
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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,
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra,

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

Regards
Raghavendra
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra,

I got it now.

Regards
Raghavendra
 
sumi selva
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic