Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Can I use an enum type in Java switch statement?

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 398
    Comment on it

    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();
       }
    
    }
    

    The above code demos the use of switch and enums.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: