Reputation: 2037
enum Enum1
{
BIG(8), HUGE(10)
{
public String getName()
{
return "Huge";
}
public String getContry()
{
return "India";
}//additional Method
},
OVERWHELMING(16)
{
public String getName()
{
return "OVERWHELMING";
}
};
private int ounces;
public int getOunes()
{
return ounces;
}
public String getName()
{
return "Ponds";
}
Enum1(int ounces1)
{
ounces = ounces1;
}
}
class EnumAsInnerClass
{
Enum1 enumInnerClass;
public static void main(String[] args)
{
EnumAsInnerClass big = new EnumAsInnerClass();
big.enumInnerClass = Enum1.BIG;
EnumAsInnerClass over = new EnumAsInnerClass();
over.enumInnerClass = Enum1.OVERWHELMING;
EnumAsInnerClass huge = new EnumAsInnerClass();
huge.enumInnerClass = Enum1.HUGE;
System.out.println(big.enumInnerClass.getName());//Ponds
System.out.println(over.enumInnerClass.getName());//OVERWHELMING
System.out.println(huge.enumInnerClass.getName());//Huge
}
}
Consider the above example. How can I call the method getCountry for HUGE?
If there is no way to call this method, why does Java treat it as legal?
Upvotes: 9
Views: 14001
Reputation: 22977
This is not suprising at all.
When you define an enum
type (in your case Enum1
) and declare values for it, their reference type will be of the defined type (Enum1
). When you define methods within your enum
declarations, you have just created anonymous subclasses.
public enum Enum1 {
OVERWHELMING(16) {
public String getName() {
return "OVERWHELMING";
}
};
// And the remaining constructors, fields and methods
}
And you can't just normally call methods of an anonymous class from outside the definition itself. Likewise, someOtherMethod()
will not be accessible from the outside either:
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
someOtherMethod();
}
public void someOtherMethod() {
System.out.println("Some other method!");
}
};
Such methods are, however, accessible from the declaration itself, like stated in this answer.
Note: Like Joachim Sauer said in his comment, this might not be the best design. You should, for example, move your methods to the Enum1
type.
There is still a way. It is possible to access the method through reflection:
Class<?> clazz = Enum1.HUGE.getClass();
Method method = clazz.getMethod("getCountry");
System.out.println(method.invoke(Enum1.HUGE)); // Prints "India"
Upvotes: 1
Reputation: 4295
If there is no way to call this method then why do java treat as legal?
You can still call it within enum value definition. For example, change getName()
function:
// ...
HUGE(10)
{
public String getName()
{
// Call additional Method
return "Huge" + getCountry();
}
public String getContry()
{
return "India";
} // additional Method
},
// ...
Otherwise, you need to override by it to be able to call it.
Upvotes: 2
Reputation: 1500385
(As noted nearly 4 years later, my original answer was incorrect.)
You can't even call Enum1.HUGE.getCountry()
with the specific enum, which is very slightly surprising... but even if you could, you couldn't do so with an arbitrary Enum1
value, which would be more generally useful.
The solution is to stop it from being an additional method - add a getCountry()
method into Enum1
, either returning a default value or maybe throwing an exception. HUGE
can then override the method.
It would be nice if you could declare that a particular enum value implemented an interface, but it seems there's no syntax for that.
Upvotes: 10
Reputation: 3549
The enum
is a type definition, similar to a class, so Enum1
doesn't have the method getCountry()
available in its interface.
edit: Alternative solution
What you can do is define the required methods in Enum1
and override them in the individual enum values.
Upvotes: 4