enum examples

gudii9
gudii9 used Ask the Experts™
on
WhoisRIR.java
public enum WhoisRIR {
    ARIN("whois.arin.net"),
    RIPE("whois.ripe.net"),
    APNIC("whois.apnic.net"),
    AFRINIC("whois.afrinic.net"),
    LACNIC("whois.lacnic.net"),
    JPNIC("whois.nic.ad.jp"),
    KRNIC("whois.nic.or.kr"),
    CNNIC("ipwhois.cnnic.cn"),
    UNKNOWN("");

    private String url;

    WhoisRIR(String url) {
        this.url = url;
    }

    public String url() {
        return url;
    }
}
Copy
Test.java
public class Test {

    public static void main(String[] args) {

		//whois.arin.net
        System.out.println(WhoisRIR.ARIN.url());

    }

}

Open in new window


going through above code from below link

https://www.mkyong.com/java/java-enum-example/

i was not clear how i got output as below

whois.arin.net


is enum like a class?
how enum different from java class?

when we use enum

not clear on below as well
Enum + Method + Some logic
Operation.java
public enum Operation {
    PLUS,
    MINUS,
    TIMES,
    DIVIDE;

    double calculate(double x, double y) {
        switch (this) {
            case PLUS:
                return x + y;
            case MINUS:
                return x - y;
            case TIMES:
                return x * y;
            case DIVIDE:
                return x / y;
            default:
                throw new AssertionError("Unknown operations " + this);
        }
    }

}
Copy
Test.java
public class Test {

    public static void main(String[] args) {

        double result = Operation.PLUS.calculate(1, 2);
        System.out.println(result); //3.0

    }

}
Copy
4. How to use Enum
4.1 To loop a Enum object.

public class Test {

    public static void main(String[] args) {

        for (UserStatus status : UserStatus.values()) {
            System.out.println(status);
        }

    }

}
Copy
Output

PENDING
ACTIVE
INACTIVE
DELETED

Open in new window

please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeffrey Dake Senior Director of Technologyy

Commented:
Enums are sort of like classes.  The are good to use as predefined constants.  They are very much like classes, except you can think of it as the values can only be values is predefined values in the enum.  

So in your example above since you are calling
 System.out.println(WhoisRIR.ARIN.url());

Open in new window

it is looking at the WhoisRIR enum and looking at the ARIN defined value.  Then since this "constant" is an object it is calling the url() function on it, which is returning the value associated with that enum.

ARIN("whois.arin.net"),

Open in new window


Here is a little more information on enums. (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)

Author

Commented:
Then since this "constant" is an object it is calling the url() function on it, which is returning the value associated with that enum.

ARIN("whois.arin.net"),

how we know constant is object or primitive ?

can we pass custom object also?
Jeffrey Dake Senior Director of Technologyy

Commented:
Almost everything in java is an object.  There are very few primitive values (int, char, double).  If you see in your code the enum is defined very similarly to a class where it can have functions within it.  Your example declared

public String url() {
        return url;
    }

Open in new window

Author

Commented:
say i have enum ServiceType which i am using inside NotificationData object


public enum ServiceType {

      SEND("555"),
      RECIEVE("666");
private final String ServiceTypeCode;
//getter of ServiceTypeCode
//setter of ServiceTypeCode
private static final Map<String, ServiceType> xYZ_MAP=new HashMap<String, ServiceType>
fromCode(String ){xyzRequestCode}
static{
return xYZ_MAP.get(xYZ_MAP)
}

public class NotificationData{
privateString SeriveType
//setter SeriveType
// getter SeriveType

i say as above and

in unit test i say

notificationData.setServiceType(ServiceType.SEND;

i am bit confused with hashmaps and static blocks and enums all the confusing things here
any good end to end examples, videos using all kinds of these stuff?
please advise
Senior Director of Technologyy
Commented:
A static block basically means you don't have to instantiate an object to reference it.  Enums are implicitly static, meaning you can reference them anywhere without having to declare them as static.  

The hashmap is a data structure used to organize data within your program. It is mapping key, value pairs and the key is based on how the objects hashCode function.  

So in your program you are mapping a String object (the key) to an ServiceType enum (the value).  This allows you later to look up the Service Type based on which String you are using.  

For your earlier question about what is a good example of an enum, the day of the week is a good example that was added in Java 8.  (https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html).  Before that programmers would make there own constants for the days of the week using ints.  Then they would have to make custom functions to do any of the functionality that the enum provides.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial