aspose file tools*
The moose likes Beginning Java and the fly likes Enums and the toString method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums and the toString method" Watch "Enums and the toString method" New topic
Author

Enums and the toString method

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Folks...

I was trying out a certain exercise from the book 'murach's Java SE6'. It's question 5 of esercise 9-4.The question from the book is as follow:

"Add a toString method to the CustomerType enumeration. This method should return a string that contains "Retail Customer", 'Trade Customer" or "College

customer" depending on the customer type. Compile this class , then run the CustomerTypeApp class again to view the results of te toString method."

My code are as follow:

CustomerType



CustomerTypeApp



The question asks to return an appropriate string based on the customer type. I haven't gone to that extend of returning based on the customer type as I seem to be having problem returing via the toString method. I understand that I cannot instantiate the enum in the 'CustomerTypeApp' class, how than do I call the toString method from withing the 'CustomerTypeApp' class? I hope someone can advise. Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Varnam Aayiram wrote:
I understand that I cannot instantiate the enum in the 'CustomerTypeApp' class, how than do I call the toString method from withing the 'CustomerTypeApp' class? I hope someone can advise. Thanks.


CustomerType.RETAIL.toString() ?


or if you have it assigned to a variable...

private CustomerType ct = CustomerType.RETAIL;

then ct.toString() ?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

If you need a method to return "Retail Customer", 'Trade Customer" or "College customer" , depending on the Customer Type you will need to associate "Retail Customer", 'Trade Customer" or "College customer" with each of the Customer type

i.e : RETAIL => "Retail Customer" , TRADE => "Trade Customer" and COLLEGE => "College Customer".

You can do that by using Constructors for Enum and return the associated values with toString() method.

The toString() method will look as follows:

String toString(CustomerType c)
{
return(c.type);
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

That's not a toString() method, that's a toString(CustomerType) method. It will not be called when you try to print out one of the enum constants itself. But yes, the idea is good:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

@ Rob: you are right , i did not override the toString() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enums and the toString method