I have a problem with static problem, this program doesn't compile. The compiler says
Enums.java:27: non-static variable this cannot be referenced from a static context
SexEnum a = new SexEnum(Sex.MALE);
^
Enums.java:28: non-static variable this cannot be referenced from a static context
SexEnum b = new SexEnum(Sex.FEMALE);
However, if I add a "static" key word to the following line, everything goes fine.
public static class SexEnum{
////////////////////////////////////////////////////////////////////////////////////
public class Enums{
enum Sex{MALE, FEMALE};
public class SexEnum{ // something wrong this line
private Sex sex;
public SexEnum(Sex sex){
this.sex = sex;
}
public void info(){
Sex s = this.sex;
switch(s){
case MALE:
System.out.println("I'm a male.");
break;
case FEMALE:
System.out.println("I'm a female.");
break;
default:
System.out.println("I'm not a human.");
break;
}
}
}
public static void main(String[] argv){
SexEnum a = new SexEnum(Sex.MALE);
SexEnum b = new SexEnum(Sex.FEMALE);
a.info();
b.info();
}
}
Can someone explain this to me? Thank you.