java2db

Use of java enum keyword with an example



The java enum type is a special data type that enables for a variable to be a set of predefined constants. The enum declaration defines a class (called an enum type). You should use java enum types any time you need to represent a fixed set of constants.
java enum keyword All enums implicitly extend java.lang.Enum. So it cannot extends any other class.
java enum keyword Declare the names of an enum type’s fields are in uppercase letters (recommended).
java enum keyword The constructor for an enum type must be package-private or private access. It automatically

…….creates the constants that are defined at the beginning of the enum body. We cannot invoke an

…….enum constructor of our own.
java enum keyword The enum class body can include methods and other fields.The compiler automatically adds some …….special methods when it creates an enum.

 

 Example for java enum :

 Execution result :

java enum keyword From the above example ,we added zero argument constructor (enumExe() ) for EXAMRESULT (because it’s not having any value).

java enum keyword The enumExe(int marks) constructor created for the remaining values(ENGLISH(80), MATHEMATICS(90), SCIENCE(85), GENERAL(70)).

java enum keyword Create appropriate constructor(s) for the field(s) that you are declared.Otherwise it throws The constructor is undefined error.




Related Posts :
   [will not be published]



^ <