You are on page 1of 5

Enum:

Is used to define group of user defined constant values

ex: enum Months {Jan,Dec;}

";" is optional in enum declaration


came in java 1.5

every enum is implemented using class concept


every enum constant is public static final
every enum constant is object of type enum
in this example Jan and Dec is Months object

Internal Implementation:
-------------------------
class Months {
public static final Months Jan = new Months();
public static final Months Dec = new Months();
}
---
Access Enum:
-----------

psvm{
Months m = Months.Jan;
sop(m);
}

enum can be declared inside class and outside class but not inside method

valid modifiers if enum declared outside the class


public,default,strictfp enum {} class {}
--------------------------------------------------------
class {
public,default,strictfp,private,protected,static enum {}
}
----------------------------
enum is direct child class of java.lang.enum
it doesnt support extend keyword
enum is always final implicitly for any enum we cant create child enum

enum can implement interface

to list all values inside enum


Months[] m = Months.values();
Values() method is not present in java.lang.enum or object classes
enum keyword implicitly provides this method

---------------------------------------------------------
ordianl()
---------------
for enum order of constant is important
we can represent this order by using ordinal value
----------------------------------------------------

enum can have var,method, main method,constructor


it can be executed directly from command line
enum Months {
jan,feb;

psvm(){
sop();
}
}
in addition to constant if we are taking extra members semicolon is mandatory
enum should be in first line

inside enum if we are taking extra members is should have constants if not atleast
";"

enum Months {
;
psvm(){
sop();
}
}
-----------------------------------------------------

empty enum is valid java syntax

enum Months {}
----------------------------------------------------
enum Months {
jan,feb
Months(){
sop("Enum Constructor");
}
}
public class Test {
psvm (string[] args){
Months m = Months.jan;
sop("test");
}
}

Here If we compile javac Test.java


two class files will be created one for enum and another for Test class
since enum has constructor defined
irrespective of number of enum object we initialized the constructor will be
executed for n objects inside enum

in the above case constructor will be executed two times and sop will get
executed

-----------------------------------------------------------------------------------
----------------------------------------------

Months m = new Months(); wrong syntax


enum constructor cannot be initialized directly
----------------------------------------------------------
Enum with constructor;
---------------------
enum Beer {
KF(90),Fo(12),RC();
int price;
Beer(int price){
this.price = price;
}
Beer(){

}
public int getPrice{
return price;
}
}

public class BeerDriver {

psvm(){
Beer b = Beer.values();
for(Beer b1 : b){
syso(b1 +""+b1.getPrice());
}
}
}

KF(90) --> public static final Beer KF = new Beer(90);


KF() --> public static final Beer KF = new Beer();
--------------------------------------------------------------------------------

Inside enum we can declare methods but should be concrete methods


------------------------------------------------------------------------------

Case 1 :
----------

Every enum constant is an object whatever method we can apply on normal java
object applicable to enum as well

Beer.KF.equals(Beer.KF);
Beer.KF.hashcode();
Beer.KF.ordinal() < Beer.KF.ordinal();
--------------------------------------------------------------

enum Beer {

kf,gv;
public void m1(){
sop("beernames");
}
}

public class BeerDriver {


psvm (){
Beer b = Beer.values();
for(Beer b1 : b){
sop(b1.m1());
}
}
}
o/p:
beernames
beernames
---------------------------------------------------------------------------
if we want to execute specific meothods for an enum then enum should be declared
like below

enum Beer {
kf, rc {
public void m2 (){
sop();
}
},RD;
}
--------------------------------------------------------------------

You might also like