Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Using enum with switch statements

Java Examples - Using enum and switch Statements

Java Examples Java Examples

Creating an enum type in Java uses the enum keyword; it is implied that the created types are all subclasses of the java.lang.Enum class

Example code for iterating over an enum and performing a switch operation:

Main.java File

enum

Car

{

lamborghini

,

tata

,

audi

,

fiat

,

honda

}

public

class

Main

{

public

static

void

main

(

String

args

[

]

)

{

Car

c

;

c

=

Car

.

tata

;

switch

(

c

)

{

case

lamborghini

:

System

.

out

.

println

(

"

You chose lamborghini!

"

)

;

break

;

case

tata

:

System

.

out

.

println

(

"

You chose tata!

"

)

;

break

;

case

audi

:

System

.

out

.

println

(

"

You chose audi!

"

)

;

break

;

case

fiat

:

System

.

out

.

println

(

"

You chose fiat!

"

)

;

break

;

case

honda

:

System

.

out

.

println

(

"

You chose honda!

"

)

;

break

;

default

:

System

.

out

.

println

(

"

I don't know your car model.

"

)

;

break

;

}

}

}

The output of the above code is:


你选择了 tata!

Java Examples Java Examples