amcc
amcc

Reputation: 2678

Java enum and nested classes

I'm a beginner with Java (Objective-C is my most well known language) and I'm really struggling to locate a clean example of what I'm trying to achieve:

public class OuterClass extends ClassToExtend {

    public enum Enum { value1, value2 }

    public class InnerClass extends AnotherClassToExtend {

        public void aMethod(int position) {
            switch (position) {
                case Enum.value1:
                    //Do something
                case Enum.value2:
                    //Do Something else
            }
        }
    }
}

Obviously this does not work (the references to the enum) but should give an idea of what I am trying to do, what needs changing to make this work? I think I need to declare the enum somewhere to reference it?

I would be grateful for an explanation of the corrections so that I can learn and hopefully solve similar difficulties for myself in the future.

Thanks

EDIT: What if the method was an @Override and the signature could not be changed?

Upvotes: 1

Views: 2278

Answers (4)

Uwe Plonus
Uwe Plonus

Reputation: 9954

You cannot switch on an int and then try to match this with an enum.

If you change you method signature to

public void aMethod(Enum position)

and use the bare enum values (e.g. value1) then it should work:

case value1:

Upvotes: 2

Waqas Memon
Waqas Memon

Reputation: 1249

your aMethod should not use int as argument type. Prior to JDK6, it was also not possible to use Strings in Switch Statement. Switch statements with String cases have been implemented in Java SE 7, at least 16 years after they were first requested.

You should also not forget break statements after each case. Otherwise, all your cases will be read.

public void aMethod(Enum position) {
 switch (position) {
                case value1:
                    System.out.println("I am Value 1");
                    break;
                case value2:
                    System.out.println("I am Value 1");
                    break;
 }
} 

to test it, use

ob.aMethod(Enum.value1);

or

ob.aMethod(Enum.valueOf("value1"));

Upvotes: 0

NimChimpsky
NimChimpsky

Reputation: 47290

override from an abstract class requiring the method with the int paramater

I would do this :

public enum Enum { value1, value2 }
public class InnerClass  {
  public void aMethod(int position) {
    Enum value = Enum.values()[position];
    switch (value) {
      case value1:

Upvotes: 2

David
David

Reputation: 20063

Firstly, you must switch on the enum itself, so you need to change the following line:

public void aMethod(int position) {

To accept your enum instead of an int. Secondly, the values of the enum need to be referenced directly within the switch, as the enum is already in the scope of the current object.

public class OuterClass extends ClassToExtend {

    public enum Enum { value1, value2 };

    public class InnerClass extends AnotherClassToExtend {

        public void aMethod(Enum position) {
            switch (position) {
                case value1:
                    //Do something
                case value2:
                    //Do Something else
            }
        }
    }
}

Upvotes: 3

Related Questions