Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Enum Syntax and Usage | Getting Started with Enums
Working with Java Enum

bookEnum Syntax and Usage

Enums in Java are a special type that lets you define a fixed set of constants. You can declare enums either as standalone types or nested inside classes. Declaring an enum outside a class is similar to declaring a regular class or interface. For example, you might write public enum Day { ... } at the top level of a file. Enums can also be declared inside another class, either as a static or non-static type, depending on your needs.

When naming enums and their constants, follow these best practices:

  • Enum type names should use UpperCamelCase (e.g., TrafficLight, DayOfWeek);
  • Enum constant names should use ALL_UPPER_CASE_WITH_UNDERSCORES (e.g., RED, YELLOW, GREEN);
  • Enum types are typically declared as public if they need to be used outside their package, or with package-private visibility otherwise.

Enums provide a clear way to represent a group of related constants and make your code more robust, readable, and self-documenting. By using enums, you avoid "magic numbers" and string constants scattered throughout your code.

Main.java

Main.java

copy
123456789101112131415161718192021222324252627
package com.example; public class Main { public enum TrafficLight { RED, YELLOW, GREEN } public static void reactToLight(TrafficLight light) { switch (light) { case RED: System.out.println("Stop!"); break; case YELLOW: System.out.println("Get ready..."); break; case GREEN: System.out.println("Go!"); break; } } public static void main(String[] args) { TrafficLight current = TrafficLight.RED; reactToLight(current); } }

Using enums as method parameters lets you restrict input to a predefined set of values, making code both safer and easier to understand. For example, when you pass a TrafficLight enum to a method, you guarantee that only valid traffic light states are used, preventing invalid or unexpected values. This improves code readability because the method signature clearly communicates what kind of values are expected, and the compiler enforces this constraint, reducing bugs.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public enum TrafficLight { RED, YELLOW, GREEN } public static void main(String[] args) { for (TrafficLight light : TrafficLight.values()) { System.out.println(light); } } }

1. What method allows you to iterate over all values of an enum?

2. Where can enums be declared in Java?

3. Which naming convention is recommended for enum constants?

question mark

What method allows you to iterate over all values of an enum?

Select the correct answer

question mark

Where can enums be declared in Java?

Select the correct answer

question mark

Which naming convention is recommended for enum constants?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show me an example of how to declare an enum in Java?

How do I use an enum as a method parameter?

What are the benefits of using enums instead of constants?

bookEnum Syntax and Usage

Scorri per mostrare il menu

Enums in Java are a special type that lets you define a fixed set of constants. You can declare enums either as standalone types or nested inside classes. Declaring an enum outside a class is similar to declaring a regular class or interface. For example, you might write public enum Day { ... } at the top level of a file. Enums can also be declared inside another class, either as a static or non-static type, depending on your needs.

When naming enums and their constants, follow these best practices:

  • Enum type names should use UpperCamelCase (e.g., TrafficLight, DayOfWeek);
  • Enum constant names should use ALL_UPPER_CASE_WITH_UNDERSCORES (e.g., RED, YELLOW, GREEN);
  • Enum types are typically declared as public if they need to be used outside their package, or with package-private visibility otherwise.

Enums provide a clear way to represent a group of related constants and make your code more robust, readable, and self-documenting. By using enums, you avoid "magic numbers" and string constants scattered throughout your code.

Main.java

Main.java

copy
123456789101112131415161718192021222324252627
package com.example; public class Main { public enum TrafficLight { RED, YELLOW, GREEN } public static void reactToLight(TrafficLight light) { switch (light) { case RED: System.out.println("Stop!"); break; case YELLOW: System.out.println("Get ready..."); break; case GREEN: System.out.println("Go!"); break; } } public static void main(String[] args) { TrafficLight current = TrafficLight.RED; reactToLight(current); } }

Using enums as method parameters lets you restrict input to a predefined set of values, making code both safer and easier to understand. For example, when you pass a TrafficLight enum to a method, you guarantee that only valid traffic light states are used, preventing invalid or unexpected values. This improves code readability because the method signature clearly communicates what kind of values are expected, and the compiler enforces this constraint, reducing bugs.

Main.java

Main.java

copy
123456789101112131415
package com.example; public class Main { public enum TrafficLight { RED, YELLOW, GREEN } public static void main(String[] args) { for (TrafficLight light : TrafficLight.values()) { System.out.println(light); } } }

1. What method allows you to iterate over all values of an enum?

2. Where can enums be declared in Java?

3. Which naming convention is recommended for enum constants?

question mark

What method allows you to iterate over all values of an enum?

Select the correct answer

question mark

Where can enums be declared in Java?

Select the correct answer

question mark

Which naming convention is recommended for enum constants?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt