Java Enum When do I need to use it?

Java Enum When do I need to use it?

what is the Java Enum in java ?

A?Java Enum?is a special Java type used to define collections of constants. More precisely, a Java enum type is a special kind of Java class. An enum can contain constants, methods etc. Java enums were added in Java 5.

what the case i can use Java enum ?

i can use java Enum with Switch statement , and when i want use json object and map it with java class object?

implementation:

how i can create Enum class , look at the following code.

public enum Level {
    HIGH,
    MEDIUM,
    LOW
}        

in the above code,?we are create new enum class, and you can see we are replace class or interface keyword to enum keyword to declaration it.

okay but what is the difference between enum and class

each value inside enum is static and final i cant change and i can call by enum name without use object it but i can add new value inside enum .

how i can represent enum value inside class , look at the following example to see what is the difference between them .

public enum Level {
    HIGH,
    MEDIUM,
    LOW
}

public class Level{
  
    public static final HIGH="HIGH";
    public static final MEDIUM="MEDIUM";
    public static final LOW="LOW";    
    }        


where i can use it

  • i can use it in switch statements look at the following code .

Level level=Level.HIGH; // here we are declration object of Enum
switch (level) {
    case HIGH   : ...; break;
    case MEDIUM : ...; break;
    case LOW    : ...; break;
}             

  • i can user inside if statements look at the following code.

Level level = Level.LOW // here we are declration object of Enum

if( level == Level.HIGH) {

} else if( level == Level.MEDIUM) {

} else if( level == Level.LOW) {

}        

okay how i can iteration in Enum

we can make iteration in enum and make print all value inside it how i can make it ,look at the following example .

for (Level level : Level.values()) {
    System.out.println(level);
}        

okay the finally about enum how i can print enum as a string or set enum value inside string variable.

we can print enum as a string and we can set inside a string value how i can make it ,look at the following example .?

String levelText = Level.HIGH.toString();
System.out.print(levelText); // HIGH        

in the above example we are set the enum value inside string variable and after that i printed it .

conclusion:

in this article i explained about enum and make new enum and make some process to it in the next article i will explain about how i can serialize enum from json and explain why i need do it?

reference:

https://tutorials.jenkov.com/java/enums.html

https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

要查看或添加评论,请登录

Abd-Alrhman Alkraien的更多文章

  • Base Entity and Audit entity with?JPA

    Base Entity and Audit entity with?JPA

    Introduction: When creating any web application and wanting to connect it with DB, we will add id as a column inside…

    1 条评论
  • Java 8 — Stream APIs

    Java 8 — Stream APIs

    overview When Java 8 came into this world, it comes to provide us with more features, flexibility, and comfortable to…

  • Java with Jackson and JSON

    Java with Jackson and JSON

    Introduction: every developer knows about JSON, and many developers use Json format with API requests or responses. So…

    2 条评论
  • Liquibase Tutorial

    Liquibase Tutorial

    Introduction: Sometimes we need to create a database and put fake data inside it, when running the application, we can…

    1 条评论
  • Element collection Vs One to Many in JPA and hibernate

    Element collection Vs One to Many in JPA and hibernate

    Introduction: Inside any project will have a database for saving and fetching the data from it, and will save the data…

  • Java history from Java 1.0 to Java 18

    Java history from Java 1.0 to Java 18

    Introduction: From this article, you can see the version of java from the first to the latest version. And what are the…

  • Immutability in Java

    Immutability in Java

    What is Immutability?!! An object is considered immutable if its state cannot change after it is constructed. Maximum…

    2 条评论
  • Logging Spring requests using aspect

    Logging Spring requests using aspect

    Introduction: In any web application we need logging request and response, We can do it using Log4j in java, but in…

  • Aspect Object Programing with Spring boot?Example

    Aspect Object Programing with Spring boot?Example

    Introduction: In the previous article, we talked about Aspect and if you read the previous article you should know the…

  • Aspect object programming with Spring?boot

    Aspect object programming with Spring?boot

    Introduction: The programming in the world is changing every day and the projects are bigger in the last few days. so…

    1 条评论

社区洞察

其他会员也浏览了