Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » » » How Constructor Works In Java Enum ?

In my last few posts

I have discussed concepts on Enum. In this post I would like to show the constructor concept in java enum.Constructor!!! that too in enum???
Yes!!! java has come up with this beautiful concept.
enum can contain constructors. This constructors will be executed at the time of enum class loading automatically for every enum constant.

enum Month    
{    
JAN,FEB,MAR;    
//note semicolon(;) is optional here    
Month()
{
System.out.println("enum constructor");
}
} 
class Testenumcons{
public static void main(String... Args)
{
Month mon=Month.JAN;
System.out.println("Main method");
}
}
output:
enum constructor
enum constructor
enum constructor
Main method
Now we can also conclude-
we can not create enum object explicitly if we do so we will get compile time error.

enum Month    
{    
JAN,FEB,MAR;    
//note semicolon(;) is optional here    
Month()
{
System.out.println("enum constructor");
}
} 
class Testenumcons{
public static void main(String... Args)
{
Month mon=new Month();
//illegal statement.
Month mon=Month.JAN;
System.out.println("Main method");
}
}
E:\myProgram\Testenumcons.java:13: error: enum types may not be instantiated
Month mon=new Month();
^
E:\myProgram\Testenumcons.java:14: error: variable mon is already defined in met
hod main(String...)
Month mon=Month.JAN;
^
2 errors
So we can not invoke enum constructor directly.
Till now in the above example we have seen no argument constructor.But we can also have argumented constructor.
enum Month    
{    
JAN(31),FEB(28),MAR(31),APR;   
int days ;
//note semicolon(;) is optional here  
//argumented constructor  
Month(int days)
{
System.out.println("enum argumented constructor ");
this.days=days;
}
// no argumented constructor
Month()
{
System.out.println("enum no argument constructor");
this.days=31;
}
public int getDays()
{
return days;
}
} 
class Testenumcons{
public static void main(String... Args)
{

Month[] mon=Month.values();
for (Month m:mon)
{
System.out.println(m+"-->"+m.getDays());
}
}
}

output:
enum argumented constructor
enum argumented constructor
enum argumented constructor
enum no argument constructor
JAN-->31
FEB-->28
MAR-->31
APR-->31
in general if you do not define any no argument constructor, Compiler will automatically put a no argument constructor inside enum when it transfer enum to class. but that will have no statement inside it. But there are two cases to check out..
Case 1. if the enum constants have mix with argumented and non argumented then it is compulsory to define both argumented and non argumented constructor. Otherwise compiler will throw compile time error.
Case-2 Compiler will automatically add no argumented constructor if no argumented constructor is defined.
enum Month    
{    
JAN(31),FEB(28),MAR(31),APR;   
int days ;
//note semicolon(;) is optional here  
//argumented constructor  
Month(int days)
{
System.out.println("enum argumented constructor ");
this.days=days;
}
// no argumented constructor

public int getDays()
{
return days;
}
} 
class Testenumcons{
public static void main(String... Args)
{

Month[] mon=Month.values();
for (Month m:mon)
{
System.out.println(m+"-->"+m.getDays());
}
}
}

E:\myProgram\Testenumcons.java:3: error: constructor Month in enum Month cannot
be applied to given types;
JAN(31),FEB(28),MAR(31),APR;
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Also inside enum we can add methods. But the methods should be concrete,not abstract.
Every enum constant represents an object of the type enum so the calling of the methods are same as the way we call normal methods
Possible operators we can use are--
  • enum.constantname.equals(enum.constantname)
  • enum.constantname==enum.constantname
  • enum.constantname.ordinal>enum.constantname.ordinal
Normal methods in enum

enum testenum21
{
JAN,FEB,MAR;
public void info()
{
System.out.println("enum info method called");
}
}

class getvalues{
public static void main(String... Args)
{
testenum21[] mon=testenum21.values();
for(testenum21 m: mon)
{
System.out.print(m.ordinal()+ " ");
m.info();

}
}
}
output:
0 enum info method called
1 enum info method called
2 enum info method called

Specific methods in enum
Let us see some specific method attached to enum.
enum testenum21
{
JAN,FEB{
public void info()
{
System.out.println("Festival of love");
}
},MAR;
public void info()
{
System.out.println("enum info method called");
}
}

class getvalues{
public static void main(String... Args)
{
testenum21[] mon=testenum21.values();
for(testenum21 m: mon)
{
System.out.print(m.ordinal()+" ");
m.info();

}
}
}
output:
0 enum info method called
1 Festival of love
2 enum info method called

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply