I apologize in advance for not knowing how to better state this question.
In the following program, I’ll get an error if I run it in NetBeans. It’s using an enum and it seems to not like it when I use the ‘this’ keyword. Code:
enum moreEnum{
JOHN(10,"nice"),
TONY(20,"cool");
String desc;
int myNum;
moreEnum (int a, String desc){
myNum = a;
this.desc = desc;
}
public String getDesc(){
return desc;
}
public static void main (String [] args){
System.out.printf("%s \t %s \n", moreEnum.JOHN, moreEnum.JOHN.desc);
System.out.printf("getDesc() method - %s", moreEnum.JOHN.getDesc());
}
}
I’ll get this error when I run it:
java.lang.VerifyError: Constructor must call super() or this() before return in method moreEnum.<init>(Ljava/lang/String;ILjava/lang/String;)V at offset 0
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Exception in thread "main" Java Result: 1
If I change the position of the line:
this.desc = desc;
to directly above the myNum = a; , then it will run fine. If I put the this.desc = desc; right back to where I moved it from (making the program identical to way I ran it the first time w/ the error), I will no longer have an error. If I then change the “desc” variable to anything else, I’ll get the error again, and can resolve it by switching the placement of the ‘this’ line, running it with no error, and switching it back and then it’ll run fine.
I guess my question is really, “What is going on?!” Is this an enum issue? ‘this’ issue? Or a NetBeans issue?
It could be a caching issue with Netbeans? I cannot reproduce this in Eclipse (JDK 1.7), unfortunately. Maybe try to clean the project before compiling/running?