Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Swtich Statement

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra,

I got it now.

Regards
Raghavendra
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic