Java Enum

“With great power comes great responsibility”.

1) Public : Whenever we declare a method as public in a class, then via objects of that class that method can be accessible inside the packages or outside the packages and also throughout the program. Also whenever we declare outer class as public then the file name must be same as the class name.

2) Final : Firstly we will see the final keyword in context with classes and then in context with methods. So whenever we declare a class as final using final keyword then no other class can extends(inherit) that class and same for methods, whenever we declare the method or variable as final then that variable or method cannot be overridden.

3) Static : Whenever we declare a method as static using static keyword in a class. then that method can be accessible outside the class without creating the objects of that class. Also when we declare the variable as static in the class and whenever we create objects of that class. Each time while creating the objects the single copy of that variable is created and divided among all objects.

1) Ordinal() : This method returns the position of constant in enum.

e.g. if we write ordinal(YELLOW) then it will return the value as 0. Because the Yellow is declared first, so according to indexing it will return 0, pink will return 1 and so on.

2) compareTo() : This will compare the constants on the basis of ordinal values and it will return the difference between ordinal values of that constants.e

e.g. Color.YELLOW.compareTo(Color.PINK)

This will return -1, because it will subtract ordinal value of PINK i.e. 1 from ordinal value of YELLOW i.e. 0, so the answer is (0–1) i.e. -1.

3) valueOf() : This method will take a string and returns an enum constant having the same string name. Suppose we write Color.valueOf(“YELLOW”) will return the constant YELLOW declared in enum.

4) values() : This method will returns an array of enum type containing all the enum constants.

E.g. Color[] constArray = Color.values();

This will store all the constants declared in enum into the constArray.

Enums are kind of class in java whereas in C/C++ enums are constant , that can be used in indexed expressions and as operands. Also C/C++ enumerations provide an alternative to the #define pre-processor directive. Enums in Java are objects — they can have methods. Enums in C/C++ enums are plain Integers. The enums are type-safe means that an enum has its own namespace, we can’t assign any other value other than specified in enum constants in C/C++ enums are not type-safe. As we talked constants in enum in C++ by default hold the integers and enums in java hold the constant name as string. Enums in C++, by default hold integers so we can use them in place of #define which is used for declaring variables which cannot be reinitialized.

Java enums are widely used in different Java applications such as spring application for mapping all the fields such as code and description fields. Another application of enum types is to represent categories of objects. It can be also used to know the download status of a file which gets downloaded from a base station.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store