Professional Documents
Culture Documents
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
---------------------------------------------------------
ordianl()
---------------
for enum order of constant is important
we can represent this order by using ordinal value
----------------------------------------------------
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();
}
}
-----------------------------------------------------
enum Months {}
----------------------------------------------------
enum Months {
jan,feb
Months(){
sop("Enum Constructor");
}
}
public class Test {
psvm (string[] args){
Months m = Months.jan;
sop("test");
}
}
in the above case constructor will be executed two times and sop will get
executed
-----------------------------------------------------------------------------------
----------------------------------------------
}
public int getPrice{
return price;
}
}
psvm(){
Beer b = Beer.values();
for(Beer b1 : b){
syso(b1 +""+b1.getPrice());
}
}
}
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");
}
}
enum Beer {
kf, rc {
public void m2 (){
sop();
}
},RD;
}
--------------------------------------------------------------------