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);
}
}
}
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.
Nested Enum TypesWe can have a nested enum type declaration inside a class, an interface, or another enum type.
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 {
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.
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); } }
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?
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With