
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Enum with Customized Value in Java
An enum in java represents a group of named constants. It can also have custom properties and methods.
Example
Let us look at an example.
import java.lang.*; // enum showing Mobile prices enum Mobile { Samsung(400), Nokia(250),Motorola(325); int price; Mobile(int p) { price = p; } int showPrice() { return price; } } public class EnumDemo { public static void main(String args[]) { System.out.println("CellPhone List:"); for(Mobile m : Mobile.values()) { System.out.println(m + " costs " + m.showPrice() + " dollars"); } Mobile ret = Mobile.Motorola; System.out.println("MobileName = " + ret.name()); } }
This will produce the following result −
Output
CellPhone List: Samsung costs 400 dollars Nokia costs 250 dollars Motorola costs 325 dollars MobileName = Motorola
Here we've added a price as field and showPrice() as method to Enum.
We've assign custom values to enum using its constructor.
- Related Questions & Answers
- Enum with Customized Value in C#
- How to sort an array with customized Comparator in Java?
- How to call another enum value in an enum's constructor using java?
- Java Program to Lookup enum by String value
- Enum in Java
- Enum Methods in Java
- Enum constructor in Java
- Comparing enum members in Java
- Create a DataFrame with customized index parameters in Pandas
- Can we create an enum with custom values in java?
- How to use an enum with switch case in Java?
- Enum in a class in Java
- Is it possible to assign a numeric value to an enum in Java?
- Implement Switch on Enum in Java
- Iterating over Enum Values in Java
Advertisements