[go: up one dir, main page]

0% found this document useful (0 votes)
44 views5 pages

Enum

it solutions

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Enum

it solutions

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

enum : java 5

enum is used to declare the group of constants.


group of cons : public static final

//before enum :
class Week
{ public static final Week MON;
public static final Week TUE;
public static final Week WED;
}

//java code with enum :


enum Week
{ MON,TUE,WED; // public static final
}

ex :
enum Week
{ MON,TUE,WED; //public static final
}
class Test
{ public static void main(String[] args)
{ Week w1 = Week.MON;
Week w2 = Week.TUE;
Week w3 = Week.WED;
System.out.println(w1+" "+w2+" "+w3);

Week[] w = Week.values();
for (Week ww : w)
{ System.out.println(ww+" "+ww.ordinal());
}
}
}

Observation :
//enum code before compilation : Week.java
enum Week
{ MON,TUE,WED;
}

//internal enum code after compilation : Week.class


public final class Week extends Enum
{ private Week(String s, int i)
{ super(s, i);
}

public static final Week MON;


public static final Week TUE;
public static final Week WED;
private static final Week VALUES[];

static
{ MON = new Week("MON", 0);
TUE = new Week("TUE", 1);
WED = new Week("WED", 2);
VALUES = (new Week[] {
MON, TUE, WED
});
}
}

1. enums internally treated as class format.


2. enums are by default final so we can not create child enums.
3. enums are by default public.
4. the default super cls of enum is : Enum
5. enum keyword to declare the enum
Enum is default super
6. enums also compiler generated .class files
7. enums contians private cons : so not possbile to create the obj
8. enum constants are by default : public static final
9. eums to declare user defined constants.
10. every cons is object

ex-2: Accessing the enums constants


a. using normal import
b. using static import

package com.tcs.enumscons;
enum Fish
{ GOLD,STAR,CAT;
}

//normal import : access the static data using class-name


package com.tcs.client;
import com.tcs.enumscons.Fish;
class TestClient
{ public static void main(String[] args)
{ System.out.println(Fish.STAR);
System.out.println(Fish.GOLD);
System.out.println(Fish.CAT);
}
}

//static import : access static data directly without using class-name


package com.tcs.client;
import static com.tcs.enumscons.Fish.*;
class MainTest
{ public static void main(String[] args)
{ System.out.println(STAR);
System.out.println(GOLD);
System.out.println(CAT);
}
}

ex 3: possible to declare the enum inside the class.


class Gmail
{ enum Mail
{ INBOX,COMPOSE,SENT;
}
public static void main(String[] args)
{
Mail[] m = Mail.values();
for (Mail mm: m)
{ System.out.println(mm);
}
}
}
ex 4 : inside the enum possible to declare constructor
enum Week
{ MON,TUE,WED;
Week()
{ System.out.println("0-arg cons");
}
}
class Test
{ public static void main(String[] args)
{ Week[] w = Week.values();
for (Week ww : w)
{ System.out.println(ww);
}
}
}

ex-5: Inside the enum it is possible to declare the parametarized constructor.


enum Week
{ MON,TUE(10),WED(10,20);
Week()
{ System.out.println("0-arg cons");
}
Week(int a)
{ System.out.println("1-arg cons");
}
Week(int a,int b)
{ System.out.println("2-arg cons");
}
}
class Test
{ public static void main(String[] args)
{
Week[] w = Week.values();
for (Week ww : w)
{ System.out.println(ww);
}
}
}

ex-6: Observation
case 1:Inside the enum if are declaring only constants these constants ends with
semicolon is optional.
enum Week
{ MON,TUE,WED
}

case 2 : Inside the enum if we are declaring constants along with some other
elements like constructor or method, in this case group of constants must be first
line must ends with semicolon.

enum Week
{ MON,TUE,WED;
Week()
{ System.out.println("0-arg cons");
}
}
ex-7:
Inside the enum it is possible to declare the
Varaibles
Methods
Constructors
instance blocks
static blocks

enum Week
{ MON,TUE(10),WED(10,20); //public static final
static
{ System.out.println("static block");
}
{ System.out.println("instance block");
}
Week()
{ System.out.println("0-arg constructor");
}
Week(int a)
{ System.out.println("1-arg constructor");
}
Week(int a,int b)
{ System.out.println("2-arg constructor");
}
public static void main(String[] args)
{ System.out.println("enum main method");
}
}

E:\>java Week
instance block
0-arg constructor
instance block
1-arg constructor
instance block
2-arg constructor
static block
enum main method

if the application contains static constants & static blocks then the execution
order is top to bottom.
first :static constants are executed :every constant is object : so instance block
& constructor executed.
second : static blocks are execcuted

//class vs. enums:


class enum
===== =====
Object Enum
support oops no oops

ex-8:
enum Day
{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
class Test
{ public static void main(String args[])
{ Day day = Day.MONDAY;
switch(day)
{ case SUNDAY: System.out.println("sunday is fun day");
break;
case MONDAY: System.out.println("monday is lazy day");
break;
default: System.out.println("other day");
break;
}
}
}
in above example we are passing enum as switch argument so the case labels must be
enums constants.

You might also like