access outer class from inner nested enum
Tag : java , By : user185283
Date : March 29 2020, 07:55 AM
This might help you 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);
}
}
|
Access Kotlin enum from Java
Tag : java , By : Big Ant
Date : March 29 2020, 07:55 AM
wish helps you I have an enum defined in a Fragment companion object: , Just in case anyone still need this: Channel.TAG_FRIENDS.getValue()
|
Java - How to access Enum Class and Enum objects via strings?
Date : March 29 2020, 07:55 AM
wish of those help You can use if-else or switch statements to determine which enum to use, and then use valueOf(): String e = "Days";
String value = "Monday";
if (e.equalsIgnoreCase(Enumerations.Days.class.getSimpleName())) {
System.out.println(Enumerations.Days.valueOf(value).getValue());
} else if (e.equalsIgnoreCase(Enumerations.Months.class.getSimpleName())) {
System.out.println(Enumerations.Months.valueOf(value).getValue());
} else {
System.out.println("Could not find enum");
}
MON
String e = "Days";
String value = "Monday";
String res = Arrays.stream(Enumerations.class.getDeclaredClasses())
.filter(c -> c.getSimpleName().equalsIgnoreCase(e))
.findFirst()
.map(c -> {
String result = null;
try {
Object o = c.getMethod("valueOf", String.class).invoke(null, value);
result = (String) o.getClass().getMethod("getValue").invoke(o);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) {
e1.printStackTrace();
}
return result;
}).orElse("Could not find");
System.out.println(res); //prints MON
|
Access enum value inside a method of enum class in Kotlin
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can use this and this.ordinal which returns the ordinal of this enumeration constant Also if you do this: fun myFunc(): Any{
val array = MyEnum.values()
println(array[this.ordinal])
println(this)
return array[this.ordinal]
}
|
Kotlin nested enum class
Tag : kotlin , By : Thaweesak Suksuwan
Date : March 29 2020, 07:55 AM
To fix this issue You need to think about how these enums will be used by external code. In essence you are creating another namespace. The first namespace is the package, and the second is the class. That may become tedious for developers to type. It also requires the class to be typed in first for your IDE autocompletion to kick in. Unless the package is saturated with too many definitions, or there are potential name conflicts, it might be better to define them outside of the class. Kotlin allows the enums to still be defined in the same file as the class if that helps keep you organized on the physical level.
|