By default, the enumeration has its own string value, and we can also assign some custom values ??to the enumeration. Consider the following example.
In Java, an enumeration (or enum for short) is a data type that consists of a fixed set of constant values. Each constant value represents a specific element of the enumeration.
By default, the values of an enum are assigned ordinal numbers starting from 0. However, you can customize the values of the enum constants by providing a value for each constant.
Here's an example of how to create an enum with customized values in Java:
enum DayOfWeek { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); private int value; private DayOfWeek(int value) { this.value = value; } public int getValue() { return value; } }
In this example, we've defined an enum called DayOfWeek
with seven constants representing the days of the week. Each constant has a customized value that we've provided in parentheses after its name. We've also added a private value
field and a public getValue()
method to the enum, which allow us to access the value of each constant.
With this custom value setup, you can retrieve the value of any constant by calling its getValue()
method. For example, you could retrieve the value of DayOfWeek.MONDAY
by calling DayOfWeek.MONDAY.getValue()
, which would return the value 1
.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.