The Artima Developer Community
Sponsored Link

Java Buzz Forum
2 Ways to Print Custom String Value of Java Enum

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
2 Ways to Print Custom String Value of Java Enum Posted: Dec 9, 2014 8:43 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: 2 Ways to Print Custom String Value of Java Enum
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
We all know that how powerful enumeration type in Java is, and one of the main strength of enum is that they can implement interface, they can have a instance variable and you can also override any method inside enum instance. In Java programs, we often need  need to convert Enum to String type, sometime just to print values in log file and other time for storing log into database.  By default, when you print an enum constant, it print its literal value e.g. if name of enum instance is RED, then it will print RED. This is also the value which is returned by name() method of java.lang.Enum class. But, there are situations when we want a custom String value for enum constant. For example, I want to print Red instead of RED when I convert Enum to String. How do you do that? Well, there are two ways you can achieve this, first by overriding toString() method for each enum constant and second by using an instance variable to hold custom String value. You can provide custom value while creating Enum constants and later you can call that method which returns custom  String value. In this article, we will see example of these two ways.
Read more »

Read: 2 Ways to Print Custom String Value of Java Enum

Topic: Money pooling based on random tiered payout Previous Topic   Next Topic Topic: Java : Collection Framework : Queue (Introduction)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use