This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Enum in java is a data type that contains fixed set of constants.
Enum can be introduce inside or outside the class.
For calling enums use this syntx enum_name.variable_name.
Enum can used with switch statement.
Enum have constructor ,methods and data members.
Enum may implement many interfaces but cannot extend any class because it internally extends Enum class.
enum week {
sun,mon,tue,wed,thu,fri,sat;
week(){
System.out.println("ENUM CONSTRUCTOR");
}
public void show(){
System.out.println("ENUM METHOD");
}
};
public class EnumExample {
public static void main(String[] args) {
System.out.println("CALLING SINGLE VARIABLE FROM ENUM : "+week.mon);
System.out.println("ENUM LENGTH : "+week.values().length);
week.sun.show();
System.out.println("CALLING ALL VARIABLE FROM ENUM : ");
for(week w : week.values()){
System.out.println(w);
}
}
}
Program Output :-
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
ENUM CONSTRUCTOR
CALLING SINGLE VARIABLE FROM ENUM : mon
ENUM LENGTH : 7
ENUM METHOD
CALLING ALL VARIABLE FROM ENUM :
sun
mon
tue
wed
thu
fri
sat
Java I/O Tutorial
No comments:
Post a Comment