• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating an enum

 
Varnam Aayiram
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

Is it possible to instantiate an enum? I have the following enum in a stand alone file, as in it's not part of of another class:

FileName: CustomerType.java





In another class I have the following code to instantiate the enum so that I can invoke it's toString method with arguments. Following is my code extract to do that:



an error says enum types cannot be instantiated. Is there any other wat to invoke the toString method with arguments? Hope someone can advise. Thanks.

 
N Sahni
Ranch Hand
Posts: 55
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Enums doesnot support public constructors and hence, cannot be instantiated.
In fact, each element in Enum like CustomerType.RETAIL are actually static instances of Enum.

You need to access toString() using any of these static instances or make the toString method static to have it accessible through CustomerType class.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No, you cannot instantiate enums, and there's a good reason for that.

Enums are for when you have a fixed set of related constants. You don't want to instantiate one, because then the set would not be fixed. If you really want to write your enum like the way you did, then you can make the toString method static, so that you don't need an instance to call it on. You could then call it like this:

But really, the code looks a little bit weird. Why does the toString method take a String as an argument? I would write the enum like this:

You then use it like this:

 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper has told you, you cannot instantiate enum members because they are already instantiated, so it is unnecessary.
There are ways you can improve your block of if-elses.But Jesper has already shown the you proper object-oriented way to do it. Far better than what I showed. Passing the argument to the constructor is correct. It also means you are overriding the toString() method rather than overloading it. Also when you have to change it, you simply addin the appropriate place, and everything will still work nicely.
And look at this:
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The advantage of Jesper's approach is that you can never forget to add a name if you add an enum constant. With if-else statements and even switch statements it's much easier to forget, especially if you use multiple if-else or switch statements.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did say what Jesper showed was far better than my switch.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic