wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Instantiating an enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating an enum" Watch "Instantiating an enum" New topic
Author

Instantiating an enum

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
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

Joined: Jul 07, 2011
Posts: 55

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.


Thanks and Regards,
Nilesh Sahni | nsahni@infocepts.com | www.infocepts.com
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20


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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Yes, I did say what Jesper showed was far better than my switch.
 
GeeCON Prague 2014
 
subject: Instantiating an enum