Загрузка...

Java Programming Tutorial enum Intro

Enum in java is a data type that contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The java enum constants are static and final implicitly. It is available from JDK 1.5. Points to remember for Java Enum enum improves type safety enum can be easily used in switch enum can be traversed enum can have fields, constructors and methods enum may implement many interfaces but cannot extend any class because it internally extends Enum class Enums are lists of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”). If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use. Point-1 All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else. Point-2 Enum in Java are type-safe: Enum has there own name-space. It means your enum will have a type for example “Company” in below example and you can not assign any value other than specified in Enum Constants. Point-3 You can specify values of enum constants at the creation time. MyEnum.values() returns an array of MyEnum’s values. Point-4 Enum constants are implicitly static and final and can not be changed once created. Point-5 Enum can be safely compare using: Point-6 You can not create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself. Point-7 Instance of Enum in Java is created when any Enum constants are first called or referenced in code. Point-8 An enum specifies a list of constant values assigned to a type. Point-9 An enum can be declared outside or inside a class, but NOT in a method. Point-10 An enum declared outside a class must NOT be marked static, final , abstract, protected , or private Point-11 Enums can contain constructors, methods, variables, and constant class bodies. Point-12 enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor. Point-13 enum constructors can have arguments, and can be overloaded. Point-14 enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized. Point-15 The semicolon at the end of an enum declaration is optional. These are legal: enum Foo { ONE, TWO, THREE} enum Foo { ONE, TWO, THREE};

Видео Java Programming Tutorial enum Intro автора Java Innovators: инновации в программировании
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки