Java

Enumeration Concept in java using Netbeans

Basic Enumeration Concepts in Java using NetBeans

Enum (Enumeration) is a set of variables defined as a constant data type, Every object of type Enum, can access the set of values ​​contained in the Enumeration. This concept is similar to Array, but Enum is a constant or fixed and type class,  Enumeration is one of the features found in the J2SE Library version 5 in Java We can define a variable of type Enum by using the keyword enum, the set of variables we define in block { } (curly braces). An object in an enum can access an attribute or method (Enum), then apply the values ​​and values ​​contained in it.


Related Articles:

In this tutorial we will learn to understand the concept of enumeration in java, maybe the use of this concept is rarely found in programs or articles that discuss java programming, here let’s look at it further.



Basic Enumeration Concepts in Java

In the following example program, we will practice how to use the enum concept in Java, in the program to be created, we need several supporting functions such as constructors, void methods, Instances / Objects, and switch-case statements. I hope you have studied it before.

Open your NetBeans IDE, then create a new project and a new class, for example, we will name the class “EnumerationConcept”. Take a look at the following source code:

Output:

Enumeration

First of all, we create an instance variable and constructor of the class. In the Enum class, we declare several variables with Zodiac names.

ZodiacSign() method is used to define the value, in each enum variable, by using the switch statement, we can determine which option will be selected for us to display or run in the main method.

After that, we call, by creating a new object from the EnumerationConcept class with the parameters in the form of options or enum variables that we will select and run.

Finally, we call the ZodiacSign() method of the new object.

Furthermore, in this second example program, we will create an enum class and then in it, there are several variables with values ​​that have been defined directly, different from the previous example.


Example: how to use for loop in Enumeration in java using NetBeans:

Output:

Enumeration

Explanation:

In the source code, we can define values ​​directly on variables in the enum class, by creating an instance variable, then creating a constructor and creating a Getter method, to return a value to the String data type of the enum variable.

ANIMAL.values() is used to display all the values ​​in the enum class, then we define it on an array object so that we can display all of its values, by looping for each method.


That’s all from me, hopefully, it will be useful.

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Back to top button