0

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.

  • 1
    It has to do with the requirements of `enum` and the fact that the class you've declared is a inner class - Take a look at [Why can't I create an enum in an inner class in Java?](http://stackoverflow.com/questions/14858036/why-cant-i-create-an-enum-in-an-inner-class-in-java) which is probably a duplicate question – MadProgrammer Jun 11 '14 at 04:37
  • @MadProgrammer It's an entirely different error message though, the enum does not appear immediately related to the issue. – user2864740 Jun 11 '14 at 04:42
  • You don't "have to add static". There are several solutions: that's one of them. You need to see [this answer](http://stackoverflow.com/a/4175901/207421) for the correct enum values. – user207421 Jun 11 '14 at 04:45
  • @user2864740 You may be right, didn't notice the OP was creating an `enum` – MadProgrammer Jun 11 '14 at 04:46

2 Answers2

3

I think you could eliminate all your problems by rolling the info method into the enum instead of making a wrapper class:

public class Enums{
    public enum Sex {
        MALE, FEMALE;

        public void info(){
            System.out.printf("I'm a %s.%n", this.toString().toLowerCase());
        }
    }

    public static void main(String[] argv){
        Sex a = Sex.MALE;
        Sex b = Sex.FEMALE;
        a.info();
        b.info();
    }
}

Java enums are classes too, so you can add methods, fields, etc.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
0

The problem is that you are trying to instanciate a inner class, and you can only do that if you do it from an instanciated object from the upper class. But if the class is static, it is not dependent from the upper class, and any other object can instanciate it.

saclyr
  • 161
  • 5