SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Java Enums: Fundamentals and Best Practices

chanda-norris avatar
12 diapositives · 279 Views · Nov 19, 2014

This guide delves into Java enums, covering their syntax, restrictions, and capabilities. We explore how to declare enums within classes, the significance of constructors, and the proper comparison methods using `==` and `equals()`. Additional insights include the compilation process of enums, best practices for defining constants, and using enums with arguments. Learn how to leverage enums effectively in your Java applications, complete with examples showcasing their power and flexibility.

Understanding Java Enums: Fundamentals and Best Practices - slide 1
01 / 12Understanding Java Enums: Fundamentals and Best Practices
1 / 17

Télécharger la présentation

Understanding Java Enums: Fundamentals and Best Practices
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal() 0 Week.Sunday “Sunday”

  2. enum • 不可繼承 • public enum Suites extenEnum{ • } • 可實作 interface • 不能放在方法中 • 所有成員為static final • 可以用==或equals來比較 • Compiler會自動加入private final 於列值中

  3. Enum • An enum specifies a list of constant values assigned to a type. • An enum is NOT a String or an int. • An enum constant's type is the enum type. • An enum can be declared outside or inside a class, but NOT in a method. • You can NEVER invoke an enum constructor directly.

  4. Enum • The enum constructor is invoked automatically, with the arguments you define after the constant value. • You can define more than one argument to the constructor, and you can overload the enum constructors • An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.

  5. Enum • enum constants can send arguments to the enum constructor, using the syntax BIG(8). • Compiling an enum generates a .class file whose name is derived from the enum's name. • The semicolon at the end of an enum declaration is optional. These are legal: • enumFoo { ONE, TWO, THREE} • enumFoo { ONE, TWO, THREE}; • Foo.values() returns an array of MyEnum's values.

  6. Declaring Enums enumProblem { BIG, HUGE, SMALL }   // this cannot be private or protectedclass MyClass {   Problem size;}public class MainClass {public static void main(String[] args) {MyClass drink = new MyClass();drink.size = Problem.BIG;           }}

  7. An example of declaring an enum inside a class • class MyClass2 {enumProblem {BIG, HUGE, SMALL }  Problem size;}public class MainClass {public static void main(String[] args) {    MyClass2 drink = new MyClass2();drink.size = MyClass2.Problem.BIG;   // enclosing class name required  }}

  8. You cannot declare enums in a method • enum Problem {BIG, HUGE, SMALL } • class MyClass { • Problem size; • } • public class MainClass { • public static void main(String[] args) { • enum Problem { BIG, HUGE, SMALL } // WRONG! • MyClass drink = new MyClass(); • drink.size = Problem.BIG; • } • } • Exception in thread "main" java.lang.Error: Unresolved compilation problem: • The member enum Problem cannot be local at MainClass.main(MainClass.java:9)

  9. 8 BIG 8 HUGE 10 SMALL 16 • enum Problem { • // 8, 10 & 16 are passed to the constructor • BIG(8), HUGE(10), SMALL(16); • Problem(int ounces) { // constructor • this.ounces = ounces; • } • private int ounces; • public intgetOunces() { • return ounces; • } • } • class MyClass { • Problem size; • public static void main(String[] args) { • MyClass drink1 = new MyClass(); • drink1.size = Problem.BIG; • MyClass drink2 = new MyClass(); • drink2.size = Problem.SMALL; • System.out.println(drink1.size.getOunces()); • for(Problem cs: Problem.values()) • System.out.println(cs + " " + cs.getOunces()); • } • }

  10. Enums may have main() methods and can be invoked as applications. • enumLightState { • RED, YELLOW, GREEN;public static void main(String[] argv) {System.out.println(LightState.RED.toString());  }}

  11. Enums have built-in name() and toString() methods, both of which return the name of the current instance. public class MainClass {public static void main(String[] argv) {System.out.println(LightState.RED.name());  }}enumLightState {  RED, YELLOW, GREEN;}

  12. You can add data, methods, and constructors to an enum. • enumSuit {  DIAMOND(true), HEART(true), CLUB(false), SPADE(false);private booleanred;  Suit(booleanb) {    red = b;  }public booleanisRed() {return red;  }public String toString() {    String s = name();    s += red ? ":red" : ":black";return s;  }}