The Answer to this question is yes, we can use enum in java with switch statement. the following examle shows how:
enum Car{THAR,XUV,SCORPIO}
class Cars {
Car car;
Cars(Car car)
{
this.car= car;
}
public void tellTheCarName()
{
switch(car)
{
case THAR:
System.out.println("the car is "+ car);
break;
case XUV:
System.out.println("the car is "+ car);
break;
case SCORPIO:
System.out.println("the car is "+ car);
break;
}
}
static public void main(String[] args) {
Cars c1=new Cars(Car.THAR);
c1.tellTheCarName();
c1=new Cars(Car.SCORPIO);
c1.tellTheCarName();
c1=new Cars(Car.XUV);
c1.tellTheCarName();
}
}
0 Comment(s)