Support Me ��


SUBSCRIBE NOW

Advanced JAVA 1


Advanced JAVA 1

Enumerations

Enumerations was added to Java language in JDK5. Enumeration means a list of named constant. In Java, enumeration defines a class type. An Enumeration can have constructors, methods and instance variables. 

It is created using enum keyword. Each enumeration constant is public, static and final by default. Even though enumeration defines a class type and have constructors, you do not instantiate an enum using new. Enumeration variables are used and declared in much a same way as you do a primitive variable.


How to Define and Use an Enumeration

1. An enumeration can be defined simply by creating a list of enum variable. Let us take an example for list of Subject variable, with different subjects in the list.

enum Subject           //Enumeration defined
{
Java, Cpp, C, Dbms
}

2. Identifiers Java, Cpp, C and Dbms are called enumeration constants. These are public, static and final by default.

3. Variables of Enumeration can be defined directly without any new keyword.

Subject sub

4. Variables of Enumeration type can have only enumeration constants as value.


Example of Enumeration

enum WeekDays
{ sun, mon, tues, wed, thurs, fri, sat }

class Test
{
public static void main(String args[])
{
WeekDays wk;
wk = WeekDays.sun;
System.out.println("Today is "+wk);
}
}

Output : Today is sun
sub = Subject.Java;
 

Values( ) and ValueOf( ) method

All the enumerations have values() and valueOf() methods in them. values() method returns an array of enum-type containing all the enumeration constants in it. Its general form is,

public static enum-type valueOf (String str)

Points to remember about Enumerations

1. Enumerations are of class type, and have all the capabilities that a Java class has.

2. Enumerations can have Constructors, instance Variables, methods and can even implement Interfaces.

3. Enumerations are not instantiated using new keyword.

4. All Enumerations by default inherit java.lang.Enum class.

Enumeration with Constructor, instance variable and Method

enum Student
{
John(11), Bella(10), Sam(13), Viraaj(9);
private int age; //age of students
int getage { return age; }
public Student(int age)
{
this.age= age;
}
}

class EnumDemo
{
public static void main( String args[] )
{
Student S;
System.out.println("Age of Viraaj is " +Student.Viraaj.getage()+ " years");
}
}
Output : Age of Viraaj is 9 years


In this example as soon as we declare an enum variable(Student S), the constructor is called once, and it initializes age for every enumeration constant with values specified with them in parenthesis.

Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.

Share this

Backlinks

Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website