Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » » ENUM Concept Simplified In Java

If the programmer wants to represent a group of constants across programming, then they can implement enum.
Enumeration. It has been introduced in Java 1.5 (or java 5 or Tiger release)

The main objective of enum is to define our own datatypes like enumerated data types. This is more powerful concept than older java constant.

enum Month
{
JAN,FEB,MAR;
//note semicolon(;) is optional here
}


or let us check a new example
enum Laptop_company
{
Lenevo,DELL,HP;
//note semicolon(;) is optional here
}


Internal Implementation of enum:

  • If we declare a enum in java compiler is going to transform that to a class concept so in the .class file that enum is structured as class. you can see a <<enum name.class>> file where you have stored your .class file.
  • If we declare element inside enum that is constant. So they are public static and final.
  • Every enum constant is an object of the type enum
Now let us see in practical how it works. Lets us take the first example..

enum Month
{
JAN,FEB,MAR;
//note semicolon(;) is optional here
}


Compiler will transform that in to the class file as..
Class Month
{
//for JAN
public static final Month JAN=new Month();
//similarly for FEB and MAR
public static final Month FEB=new Month();
public static final Month MAR=new Month();
}
Based on the discussion we can say ..
Every enum constant is always static hence we can always access them using enum name

enum Month{
JAN,FEB,MAR;
}
class testenum2
{
public static void main(String... Args)
{
Month m=Month.JAN;
System.out.println(m);
}

}

output: JAN

  • Inside every enum toString() method is internally implemented to return the name of the constant.
  • We can declare enum inside of a class and outside of a class.If we declare inside of a class compiler will treat that as inner class.
    The applicable modifiers are 1. public 2. <default> 3. strictfp 4.private 5. protected 6.static 
  • if we put enum outside of the class then compiler will treat it as different class. Hence the modifier would be public,<default>,strictfp 
so
enum Month{
JAN,FEB,MAR;
}
class testenum2
{
public static void main(String... Args)
{
Month m=Month.JAN;
System.out.println(m);
}

}

This is a valid deceleration
class testenum2
{
enum Month{
JAN,FEB,MAR;
}
public static void main(String... Args)
{
Month m=Month.JAN;
System.out.println(m);
}

}

This is also a valid deceleration.
We can not declare an enum inside of a method. If so, Compiler will throw Compile time exception.

let us check out that example..
class testenum2
{
public static void main(String... Args)
{
Month m=Month.JAN;
System.out.println(m);
}
public static void somemethod();
{
enum Month{
JAN,FEB,MAR;
}
}
}
here well get the compiler error as..
E:\myProgram\testenum2.java:11: error: enum types must not be local
enum Month{
^
1 error


When I said new enum is more powerful after 1.5 version of java.In new enum, we can add constant, method, constructor, normal variables.
Even enum can contain main method. We can also invoke enum class directly from command prompt.
enum testenum21
{
JAN,FEB,MAR;
public static void main(String... Args)
{
System.out.println("Runing enum main method");
}
}

output:
C:\Documents and Settings\anichatt.APPLICATIONS>javac E:\myProgram\testenum21.java
C:\Documents and Settings\anichatt.APPLICATIONS>java testenum21
Runing enum main method

  • In addition to constant if other members(methods) other than constant are taken, list of constant should be in the written in the first line.
  • The constants must end with semicolon.
  • No constants are allowed to write after method name.
  • If the enum is having only method inside in, then the firstline should be empty statement(i.e only semi colon) 
  • empty enum is valid in java.
let us check the standards one by one..
The first line should be constant deceleration and it must end with semicolon
enum testenum21
{
JAN,FEB,MAR;

public static void somemethod()
{
System.out.println("do something");
}
}
This syntax is valid and semicolon is mandate

Let us check out if we don't give semicolon what will happen

enum testenum21
{
JAN,FEB,MAR

public static void somemethod()
{
System.out.println("do something");
}
}
The output is :
E:\myProgram\testenum21.java:4: error: ',', '}', or ';' expected
public static void somemethod()
^
E:\myProgram\testenum21.java:4: error: '}' expected
public static void somemethod()
^
E:\myProgram\testenum21.java:7: error: class, interface, or enum expected
}
^
3 errors
Let us see how it behaves with constants to declared after method:
enum testenum21
{
public static void somemethod()
{
System.out.println("do something");
}
JAN,FEB,MAR;
}
The output: Compilation error:
E:\myProgram\testenum21.java:2: error: expected
{
^
E:\myProgram\testenum21.java:4: error: ',', '}', or ';' expected
public static void somemethod()
^
E:\myProgram\testenum21.java:4: error: '}' expected
public static void somemethod()
^
E:\myProgram\testenum21.java:7: error: class, interface, or enum expected
}
^
E:\myProgram\testenum21.java:9: error: class, interface, or enum expected
}
^
5 errors




Similar compilation error will happen if and only if methods are declared inside enum without blank statement:
enum testenum21
{
public static void somemethod()
{
System.out.println("do something");
}

}
This syntax is also invalid.
The modified version will be something like written below with an empty statement.
enum testenum21
{
;
//this semicolon means an empty statement.
public static void somemethod()
{
System.out.println("do something");
}

}
Also empty enum is valid...

enum testenum21
{

}
Final points to remember:

  • Every enum is direct child of java.lang.Enum class.
  • It is an abstract class
  • Hence it becomes a base class for java enum
  • This is also a direct child of Object
  • enum implements comparable and serializable interface


«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply