<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hitesh Kumar on Medium]]></title>
        <description><![CDATA[Stories by Hitesh Kumar on Medium]]></description>
        <link>https://medium.com/@hitesh_kumar?source=rss-22f961eb31b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I9ij0MDlQYhVOfcyVi84Ug.jpeg</url>
            <title>Stories by Hitesh Kumar on Medium</title>
            <link>https://medium.com/@hitesh_kumar?source=rss-22f961eb31b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 31 May 2026 20:42:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hitesh_kumar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[ENUMS IN JAVA]]></title>
            <link>https://medium.com/@hitesh_kumar/enums-in-java-7a58b3b13b93?source=rss-22f961eb31b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a58b3b13b93</guid>
            <dc:creator><![CDATA[Hitesh Kumar]]></dc:creator>
            <pubDate>Wed, 30 Jun 2021 04:40:36 GMT</pubDate>
            <atom:updated>2021-06-30T04:40:36.214Z</atom:updated>
            <content:encoded><![CDATA[<p>A Java enum is a data type that stores a list of constants. You can create an enum object using the enum keyword. Enum constants appear as a comma-separated list within a pair of curly brackets.</p><p>An enum, which is short for enumeration, is a data type that has a fixed set of possible values. A variable assigned from an enum can only have a value that appears in the enum. Enums help developers store data that they know will not change.</p><p><strong>Declaring a Java Enum</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1V9nGtEWkdSWVMQm" /></figure><p>Enums are declared using the “<em>enum</em>” type. Here’s the syntax of the “<em>enum</em>” keyword:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/0*vzBRBmuv5udstJca" /></figure><p>Let’s explain this syntax:</p><p>1) enum tells our program we want to declare an enumeration.</p><p>2) Name is the name of our enum</p><p>3) Value1, VALUE2, VALUE3, VALUE4 are the set of constant values our enum stores. These values are usually written in uppercase.</p><p><strong>When to use Enums?</strong></p><p>You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.</p><p>If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/0*7DafwsDS7FEZqcVc" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/118/0*e-ykw_CcyYSN1TT7" /></figure><p><strong>Some important points on Java Enum :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0ClTwX4y91w8cwSK" /></figure><ul><li>All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.</li><li>Enum has their own name-space. It means your enum will have a type for example “<em>Direction</em>” and you can not assign any value other than specified in Enum Constants.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RwUjoLoZHUEaYFfz" /></figure><ul><li>Enum can have fields, constructors, and methods.</li><li>The enum constants have an initial value which starts from 0, 1, 2, 3, and so on. But, we can initialize the specific value to the enum constants by defining fields and constructors.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jdXHlP3CwMAI45EV" /></figure><p>This code returns : Value of NORTH is 5</p><ul><li>Enum constants are implicitly static and final and can not be changed once created.</li><li>You can not create instance of enums by using a new operator in Java because the constructor of Enum in <a href="https://crunchify.com/category/java-tutorials/">Java</a> can only be private and Enums constants can only be created inside Enums itself.</li><li>Instance of Enum in Java is created when any Enum constants are first called or referenced in code.</li><li>An enum specifies a list of constant values assigned to a type.</li><li>An enum can be declared outside or inside a class, but NOT in a method.</li><li>An enum declared outside a class must NOT be marked static, final , abstract, protected , or private.</li><li>enum constructors can have arguments, and can be overloaded.</li><li>enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.</li><li>The semicolon at the end of an enum declaration is optional.</li></ul><p><strong>Java Enum Methods</strong></p><p><strong>Values():</strong></p><p>The values() method returns a <a href="https://careerkarma.com/blog/java-array/">Java array</a> which stores all the constants in an enum. Here’s an example of the values() method in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/0*eBqJvKeNKIGjzVLl" /></figure><p><strong>valueOf():</strong></p><p>valueOf() accepts a string and returns the enum constant, which has the same name. So, if we wanted to retrieve the enum constant with the name NORTH, we could do so using this code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/0*G3kKqUw8DpuIXvny" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/118/0*SXj6OyP-QkMPHwCC" /></figure><p><strong>name():</strong></p><p>The name() method returns the name used to define a constant in an enum class. Here’s an example of the name() method being used to return the defined name of NORTH Direction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*de0gU4McOaCyrWK5" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/118/0*TQPQG9LlMPx4_tPT" /></figure><p><strong>toString():</strong></p><p>toString() converts the name of an enum to a string. Here’s an example of toString() being used to convert the NORTH enum value to a string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*EcVVc-yyVo6xibMJ" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/118/0*0IfxU-fz22u37SfL" /></figure><p><strong>ordinal():</strong></p><p>toString is important in enums. By using the ordinal() method, each enum constant index can be found, just like array index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*fV4dZpggM7k12L7q" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/56/0*KbZL1AKBUvzqLuZz" /></figure><p><strong>compareTo():</strong></p><p>compareTo() compares the constants in an enum lexicographically and returns the difference between their ordinal values. Here’s an example of compareTo() being used with an enum value from our above example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gULXUuTf_VWv0vTA" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/64/0*UxMeD6U65han4XNQ" /></figure><p><strong>When to use name() and when to use toString()</strong></p><p>If you need to get the exact name used to declare the enum constant, you should use name() as toString may have been overridden.</p><p>If you want to print the enum constant in a user-friendly way, you should use toString which may have been overridden.</p><h3>Example of specifying initial value to the enum constants</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IQo9D_WDaElrJurG" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/0*yZB42nDMdCmwq2dE" /></figure><p><strong>Enums can be safely compared using:</strong></p><ol><li>== operator or equals method</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zxHvmygN2XLxQypM" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/0*t3KK0kyXthdWw_4h" /></figure><p>2. Switch-case statement</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LZAfTMWchbdo3b0x" /></figure><p>This code returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/0*OGWGVI6rIxLy4N7o" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a58b3b13b93" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>