The below code throws a lot of errors at me. So why is it not possible to have an enum like below with only the constructor so that it can be instantiated else where??
public class TestEnum{
enum Animal
{
public Animal(String name)
{
this.name = name;
}
String name;
}
}
Is there any way of instatiating enum or does it violate the very basic property/functionality of enums and it should only be used for creating a set of,say, ready-made objects??
Because an enum consists of enumerated constant values (that is constant at compile and run-time)
Your code is otherwise (almost) correct, for example
enum Animal {
Dog("Bark"), Cat("Meow"); // Dog and Cat.
Animal(String name) { // No, it can't be public.
this.name = name;
}
String name;
}
If you want dynamic values, use a class
.
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