What is correct syntax to write an enum in java?
package in.technoscience.first;
public enum Work {
Tough,Easy,VeryEasy;
}
1) Use the keyword enum which will tell that type being created is an enum.
2) Inside the curly braces, put in constant that you want to use in your application.
How compiler deal with the enum created?
First thing to remember is as it is a type in java so compiler will generate .class file with the name enum means here we will can find Work.class file.
The code generated by the compiler:
// the enum user has defined is treated as class by compiler.
// compiler attach final with class so it means it can not be subclassed
public final class Work extends Enum { //user defined enum extends java.lang.Enum
//enum value that user used in enum is considered as the constant of type Work(class name)
public static final Work Tough;
public static final Work Easy;
public static final Work VeryEasy;
private static final Work ENUM$VALUES[];// an array to store enum values
//compiler provide private constructor, another reason it can not be subclassed
private Work(String s, int i) {
super(s, i);
}
// values method is put by compiler which plays great role in iterating through enum
public static Work[] values() {
Work awork[];
int i;
Work awork1[];
System.arraycopy(awork = ENUM$VALUES, 0,
awork1 = new Work[i = awork.length], 0, i);
return awork1;
}
public static Work valueOf(String s) {
return (Work) Enum.valueOf(in / technoscience / first / Work, s);
}
//a static block is used where each value is instantiated with enum value name and index no.
static {
Tough = new Work("Tough", 0); // it represents every enum value is an instance of user defined enum type
Easy = new Work("Easy", 1);
VeryEasy = new Work("VeryEasy", 2);
ENUM$VALUES = (new Work[] { Tough, Easy, VeryEasy }); //anonymous array
}
}
Here you can visualize , for 3 lines of code you have written, how compiler deals with that !
Can we write an enum inside a class?
yes we can write an enum inside a class also.
package in.technoscience.first;
public class Uploader {
public enum UploadStatus { INITIALIZING, IN_PROGRESS, COMPLETE };
// Class body
}
Here compiler will generate two .class file
1
st :
Uploader.class
2nd: Uploader$UploadStatus.class file
code generated by compiler :
ackage in.technoscience.first;
// user defined enum as inner class inside the class where it has been defined
public class Uploader
{
public static final class UploadStatus extends Enum
{
public static final UploadStatus INITIALIZING;
public static final UploadStatus IN_PROGRESS;
public static final UploadStatus COMPLETE;
private static final UploadStatus ENUM$VALUES[];
public static UploadStatus[] values()
{
UploadStatus auploadstatus[];
int i;
UploadStatus auploadstatus1[];
System.arraycopy(auploadstatus = ENUM$VALUES, 0, auploadstatus1 = new UploadStatus[i = auploadstatus.length], 0, i);
return auploadstatus1;
}
public static UploadStatus valueOf(String s)
{
return (UploadStatus)Enum.valueOf(in/technoscience/first/Uploader$UploadStatus, s);
}
static
{
INITIALIZING = new UploadStatus("INITIALIZING", 0);
IN_PROGRESS = new UploadStatus("IN_PROGRESS", 1);
COMPLETE = new UploadStatus("COMPLETE", 2);
ENUM$VALUES = (new UploadStatus[] {
INITIALIZING, IN_PROGRESS, COMPLETE
});
}
private UploadStatus(String s, int i)
{
super(s, i);
}
}
public Uploader()
{
}
}