Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access outer class from inner nested enum

Tags:

Is There a Way to Access the Outside?

public class OuterClass  {
    String data;

    public void outerMethod(String data) {
         this.data = data;
    }

    public enum InnerEnum {
        OPTION1("someData"),
        OPTION2("otherData");

        InnerEnum(String data) {
              // Does not work:             
              OuterClass.this.outerMethod(data);
        }
    }
}
like image 540
Cel Avatar asked Dec 10 '11 16:12

Cel


People also ask

Can inner class access outer class method?

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you must first instantiate the outer class.

Can enum be nested?

Nested Enum TypesWe can have a nested enum type declaration inside a class, an interface, or another enum type.

What is nested enum in Java?

Enums can be defined as members of a class aka 'nested enum types'. Nested enum defined as member of a class. //In file Employee.java. public class Employee { enum Department {

How do you declare an inner class in Java?

The object of java inner class are part of the outer class object and to create an instance of the inner class, we first need to create an instance of outer class. Java inner class can be instantiated like this; OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.


3 Answers

As Eric said, enums are implicitly static. To do what you want, add a method, callOuterMethod(OuterClass oc) that calls oc.outerMethod(data) to do what you want:

public enum InnerEnum {     OPTION1("someData"),     OPTION2("otherData");      final String data;      InnerEnum(String data) {        this.data = data;                  }      void callOuterMethod(OuterClass oc) {         oc.outerMethod(data);     } } 
like image 147
user949300 Avatar answered Oct 07 '22 14:10

user949300


Can't do that. The enum is implicitly static, even though you didn't declare it to be. See similiar question/answer:

"Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static."

In Java, are enum types inside a class static?

like image 26
Eric Schlenz Avatar answered Oct 07 '22 15:10

Eric Schlenz


I believe you're confusing object instances with types. What you have declared is two nested types. That is not the same as two nested object instances.

The keyword this has no meaning when operating with types. It only takes on meaning when dealing with object instances. So, if you're trying to call an instance method of the outer type from the inner type then you need a reference to an instance of the outer type.

However, if you make the method of the outer type static then you can invoke the static method from the nested type without needing a reference to an instance of the outer type. Just keep in mind that if you do that, the method is "the same for all instances" - meaning that it shares any state with all instances of the OuterClass - so it can only access static members of the type.

In the example below, the outerMethod is declared static, and as such it can be called from the nested type without needing a reference to an instance of OuterClass. However, by doing that, it can no longer access the private instance member data (without a reference to an instance of course). You could declare a static member staticData and access that instead, but just keep in mind that that member will be shared by all instances of OuterClass, and by all invokations of outerMethod.

public class OuterClass  {

        String data;                 // instance member - can not be accessed from static methods
                                     //   without a reference to an instance of OuterClass

        static String staticData;    // shared by all instances of OuterClass, and subsequently
                                     //   by all invocations of outerMethod

        // By making this method static you can invoke it from the nested type 
        //  without needing a reference to an instance of OuterClass. However, you can
        //  no longer use `this` inside the method now because it's a static method of
        //  the type OuterClass
    public static void outerMethod(String data) {

            //this.data = data;  --- will not work anymore

            // could use a static field instead (shared by all instances)
            staticData = data;
    }

    public enum InnerEnum {
        OPTION1("someData"),
        OPTION2("otherData");

        InnerEnum(String data) {
                    // Calling the static method on the outer type
                    OuterClass.outerMethod(data);
        }
    }
}
like image 21
Mike Dinescu Avatar answered Oct 07 '22 13:10

Mike Dinescu