Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a template or something for generating a switch statement for Java enum in Eclipse?

Is there a template or something for generating a switch statement for Java enum in Eclipse?

So that when I got an enum and I want to have a switch with all the values, I didn't have to write all it myself?

like image 563
Touko Avatar asked May 15 '09 11:05

Touko


People also ask

Can you use a switch statement for enums?

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.

How do you switch enums in Java?

Switch on Enum Using Traditional Switch and Case in Java We get the value from the enum using the constant's name like Days. MONDAY will fetch the constant MONDAY , and it will be stored in the enum object day . We can use it to switch between cases. switch() takes in the value to switch, that is day .

How do you use enum switch case?

enum constants are accessed using dot syntax. An enum class can have attributes and methods, in addition to constants. You cannot create objects of an enum class, and it cannot extend other classes. An enum class can only implement interfaces.

Can enum type can be passed as an argument to switch statement?

Yes, You can use Enum in Switch case statement in Java like int primitive. If you are familiar with enum int pattern, where integers represent enum values prior to Java 5 then you already knows how to use the Switch case with Enum.


2 Answers

There certainly is, at least in 3.5.

Starting with something like this:

switch(a.getType()){

}

All you need to do is click on the switch keyword and hit CTRL+1. You should get a drop down which includes the option "Add missing case statements"

like image 97
Peter Recore Avatar answered Sep 23 '22 22:09

Peter Recore


The content assists in Eclipse 3.4 will help you write the code. Just type case and press Ctrl+Space and you'll get a list of unused enums.

like image 45
Aaron Digulla Avatar answered Sep 20 '22 22:09

Aaron Digulla