Is it possible to use class methods from enum in this class in Java?

Suppose I have a class that contains an enum in Java. Can methods be accessed from the class that contains the enum? As an example:

public class Foo {

   private string getClassVal() { return "42"; } 

   public string getOtherClassVal() { return "TheAnswer"; } 

   public enum Things {

         ONE("One"), 
         TWO("Two"), 
         THREE("Three"); 

         private String _val; 

         private Things(String val) { _val = val; } 

         // This method is the problem
         public String getSomeVal() {
             return _val + this.getClassVal(); 
         }

         // And this one doesn't work either
         public String getSomeOtherVal() {
             return _val + this.getOtherClassVal(); 
         }

}

      

I know that the commented enumeration methods above do not work and lead to errors due to the context in which it is located this

. What I'm looking for is where I can access the "external" methods of the class from within the enum. Or is this even the right approach?

Is something like this possible? Or enums blocked by external methods, even inside a class?

+3


source to share


1 answer


Not.

Because they enum

always static

and therefore do not have an enclosing instance.



Obviously they can access the public static

methods of the enclosing class.

+5


source







All Articles