0% found this document useful (0 votes)
349 views17 pages

Enums: Keerthana Technologies

The document discusses Java enums and provides examples of using enums. It shows how enums can be used to define a set of constants and overcome limitations of traditional integer constants. Examples demonstrate different ways to define enums with constructors, access enum values and methods, declare enums within classes, and initialize static variables in enums.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views17 pages

Enums: Keerthana Technologies

The document discusses Java enums and provides examples of using enums. It shows how enums can be used to define a set of constants and overcome limitations of traditional integer constants. Examples demonstrate different ways to define enums with constructors, access enum values and methods, declare enums within classes, and initialize static variables in enums.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

KEERTHANA TECHNOLOGIES

Enums








class StatusWithoutEnum{
public static final int STATUS_OPEN = 0;
public static final int STATUS_STARTED = 1;
public static final int STATUS_INPROGRESS = 2;
public static final int STATUS_ONHOLD = 3;
public static final int STATUS_COMPLETED = 4;
public static final int STATUS_CLOSED = 5;
}

public class StatusEnum {

}













KEERTHANA TECHNOLOGIES

The above technique has some limitations

Type safety
Compile time constants:

Uninformative

Restricted Behavior

Meaningless switch-case use:

Non iterative list:






The solution to above limitations is Java Enum.

public class RequestStatus {

private final int status;

private RequestStatus(int status){
this.status = status;
}

public static final RequestStatus STATUS_OPENED = new RequestStatus(0);
public static final RequestStatus STATUS_STARTED = new RequestStatus(1);
public static final RequestStatus STATUS_INPROGRESS = new RequestStatus(2);
public static final RequestStatus STATUS_ONHOLD = new RequestStatus(3);
public static final RequestStatus STATUS_COMPLETED = new RequestStatus(4);
public static final RequestStatus STATUS_CLOSED = new RequestStatus(5);

}




KEERTHANA TECHNOLOGIES





package Notes;

public class ReqStatus {
public enum Status{
STATUS_OPEN,
STATUS_STARTED,
STATUS_INPROGRESS,
STATUS_ONHOLD,
STATUS_COMPLETED,
STATUS_CLOSED;
}
public static void main(String[] args) {
for(Status stat : Status. values()){
System.out.println(stat);
}
}

}

Output

STATUS_OPEN
STATUS_STARTED
STATUS_INPROGRESS
STATUS_ONHOLD
STATUS_COMPLETED
STATUS_CLOSED















KEERTHANA TECHNOLOGIES
Different uses of Java Enums



enum Season4{

SPRING(), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
System.out.println("value of f = " +f);
}

Season4(String s){
System.out.println("value of s = " +s);
}

Season4(){
System.out.println("default constructor");
}
}
public class O {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
}

}

Output:
main begin
default constructor
value of f = 20.0
value of s = Keerthana
default constructor







KEERTHANA TECHNOLOGIES


enum Season4{

SPRING(10), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
System.out.println("value of f = " +f);
}

Season4(String s){
System.out.println("value of s = " +s);
}

Season4(){
System.out.println("default constructor");
}
}
public class O {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
Season4 s2 = Season4.SPRING;
Season4 s3 = Season4.SUMMER;
Season4 s4 = Season4.WINTER;
}
}

Output:
main begin
value of i =10
value of f =20.0 value of s = Keerthana
default constructor





















KEERTHANA TECHNOLOGIES


enum Season4{

SPRING(10), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
this("Pallavi");
System.out.println("value of f = " +f);
}

Season4(String s){
this();
System.out.println("value of s = " +s);
}

Season4(){
this(20);
System.out.println("default constructor");
}
}

public class O {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
Season4 s2 = Season4.SPRING;
Season4 s3 = Season4.SUMMER;
Season4 s4 = Season4.WINTER;
}

}
Output:
main begin
value of i =10
value of i =20
default constructor
value of s = Pallavi
value of f = 20.0
value of i =20
default constructor
value of s = Keerthana
value of i =20
default constructor




KEERTHANA TECHNOLOGIES

enum Season4{

SPRING(10), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
this("Pallavi");
System.out.println("value of f = " +f);
}

Season4(String s){
this();
System.out.println("value of s = " +s);
}

Season4(){
this(20);
System.out.println("default constructor");
}
}

public class O1 {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
System.out.println("------");
Season4 s2 = Season4.SPRING;
System.out.println("----------");
Season4 s3 = Season4.SUMMER;
System.out.println("----------");
Season4 s4 = Season4.WINTER;
}

}
Output:
main begin
value of i =10
value of f = 20.0
value of s = Keerthana
default constructor
------
----------
---------

KEERTHANA TECHNOLOGIES

enum Season4{

SPRING(10), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
this("Pallavi");
System.out.println("value of f = " +f);
}

Season4(String s){
this();
System.out.println("value of s = " +s);
}

Season4(){
this(20);
System.out.println("default constructor");
}
}

public class O {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
System.out.println("------");
Season4 s2 = Season4.SPRING;
System.out.println("----------");
Season4 s3 = Season4.SUMMER;
System.out.println("----------");
Season4 s4 = Season4.WINTER;
}

}
Output:
main begin
value of i =10
value of f = 20.0
value of i =20
default constructor
value of s = Keerthana
value of i =20
default constructor
------
----------
----------

KEERTHANA TECHNOLOGIES

enum Season4{

SPRING(10), SUMMER(20.0f), FALL("Keerthana"), WINTER;

Season4(int i){
System.out.println("value of i =" +i);
}

Season4(float f){
System.out.println("value of f = " +f);
}

Season4(String s){
System.out.println("value of s = " +s);
}

Season4(){
System.out.println("default constructor");
}
}

enum Season5{
SPRING,SUMMER,FALL,WINTER;
Season5(){
System.out.println("default constructor");
}
}

public class O {
public static void main(String[] args) {

System.out.println("main begin");
Season4 s1 = Season4.FALL;
System.out.println("-------");
Season5 s5 = Season5.SPRING;

}

}









KEERTHANA TECHNOLOGIES

Output:
main begin
value of i =10
value of f = 20.0
value of s = Keerthana
default constructor
-------
default constructor
default constructor
default constructor
default constructor

We can declare any number of enum within the same class.

public class A1 {
enum Season{
SPRING,SUMMER,FALL,WINTER;
interface A2{
void print();
void print1();
}
}

public static void main(String[] args)
{
Season s1 = Season. FALL;
Season s2 = Season. SPRING;
Season s3 = Season. FALL;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
A3 a3 = new A3();
a3.print();
}

}

import enum1.A1.Season.A2;

public class A3 implements A2 {
public void print(){
System.out.println("Hello");
}

}
FALL
SPRING
FALL
Hello



KEERTHANA TECHNOLOGIES


public class A2 {

void print(){
enum Days{
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;
}
}

public static void main(String[] args) {

}

}

SUNDAY

abstract class A3{
enum Days{
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;
}
}

public class A2 extends A3{
public static void main(String[] args) {
Days d1 = Days. SUNDAY;
Days d2 = Days. SATURDAY;
System.out.println(d1);
System.out.println(d2.ordinal());
}
}

SUNDAY
6



















KEERTHANA TECHNOLOGIES


public class A4 {

enum Enum1{
SPRING, SUMMER, FALL, WINTER;

abstract class A5{
abstract void display();
void display1(){
System.out.println("Keerthana Technologies");
}
}
}
public static void main(String[] args) {
Enum1 s1 = Enum1.SPRING;
A6 a6 = new A6();
a6.display();
a6.display1();

}
}

import enum1.A4.Enum1.A5;

public class A6 extends A5 {
A6 a6 = new A6();
void display(){
System.out.println("groups");
}



}
No enclosing instance of type A4.Enum1 is available due to some
intermediate constructor invocation



KEERTHANA TECHNOLOGIES


public class C1 {

enum Pallavi{
MALE,FEMALE;
static int x;
int y =30;

static{
System.out.println("SIB");
}

{
System.out.println("IIB");
}

static void m1(int...s){
System.out.println("int");
}

void m2(){
System.out.println("hello");
}
}

public static void main(String[] args) {
Pallavi p1 = Pallavi.FEMALE;
System.out.println(Pallavi.FEMALE.ordinal());
System.out.println(p1.y);
System.out.println(p1.x);
p1.m1();
p1.m2();
}
}
Output :
IIB
IIB
SIB
1
30
0
int
hello

KEERTHANA TECHNOLOGIES

public class A2 {

void print(){
enum Days{
SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY;
}
}

public static void main(String[] args) {

}
}

Output :
C

public class B2 {

enum Season{
SPRING,SUMMER,FALL,WINTER;
}
public static void main(String[] args) {
Season s1 = Season. SPRING;
Season s2 = Season. SUMMER;
Season s3 = Season. FALL;
Season s4 = Season. WINTER;

switch(s3){
case SPRING :
System.out.println("I am SPRING");
break;
case SUMMER :
System .out. println("I am SUMMER");
break;
case FALL:
System.out.println("I am FALL");
break;
case WINTER:
System.out.println("I am WINTER");
break;
default:
System.out.println("NOTHING");
break;
}
}
}
I am FALL

KEERTHANA TECHNOLOGIES

values():
valueOf():
Ordinal():
name():
public class A {
enum Month{
JANUARY,FEBRAUARY,MARCH,APRIL,MAY,JUNE,JULY,
AUGUST,SEPTEMBER,OCTOMBER,NOVEMBER,DECEMBER;
}

public static void main(String[] args) {
Month m1[] = Month. values();
for(Month temp: m1){
System.out.println(temp);
}
}
}

JANUARY
FEBRAUARY
MARCH
APRIL
MAY
JUNE
JULY
AUGUST
SEPTEMBER
OCTOMBER
NOVEMBER
DECEMBER




















KEERTHANA TECHNOLOGIES

public class B {
enum Nature{
TREE,STONE,WIND,WATER,FIRE;
}

public static void main(String[] args) {
Nature n1 = Nature.valueOf("TREE");
Nature n2 = Nature.valueOf("STONE");
Nature n3 = Nature.valueOf("WIND");
System.out.println("n1=" +n1+ "n2 = " +n2+ " n3 = " +n3);
}
}
n1 = TREE n2 = STONE n3 = WIND
package enum1;

public class B1 {
enum Boolean{
FALSE,TRUE
;
abstract class B2{
void print(){
System.out.println("Hello");
}
abstract void print1();
}
}
public static void main(String[] args) {
System.out.println(Boolean. TRUE);
System.out.println(Boolean.FALSE.ordinal());
}
}
Output: True 0
















KEERTHANA TECHNOLOGIES

import Note.A.Month;

public class C {

enum Month1{
JANUARY,FEBRAUARY,MARCH,APRIL,MAY,JUNE,JULY,
AUGUST,SEPTEMBER,OCTOMBER,NOVEMBER,DECEMBER;
}

public static void main(String[] args) {
Month1 m1 = Month1.JANUARY;
Month1 m2 = Month1.JULY;
String s1 = m1.name();
System.out.println(s1);
System.out.println(m2.ordinal());
System.out.println(Month.AUGUST.ordinal());
System.out.println(Month. valueOf("APRIL"));

}

}

JANUARY
6
7
APRIL

You might also like