{"id":1425,"date":"2015-06-21T20:37:18","date_gmt":"2015-06-21T15:07:18","guid":{"rendered":"https:\/\/theoryofprogramming.wordpress.com\/?p=1425"},"modified":"2015-06-21T20:37:18","modified_gmt":"2015-06-21T15:07:18","slug":"java-tutorials-enum-and-methods-in-java","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2015\/06\/21\/java-tutorials-enum-and-methods-in-java\/","title":{"rendered":"Java Tutorials &#8211; Enum and Methods in Java"},"content":{"rendered":"<p>Hello people..! This is a new post in Java Tutorials &#8211; Enum and Methods in Java. In this post I will talk about <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> and methods (that is, functions) in Java. The full potential and usage of these constructs will be seen when we start creating classes. But you&#8217;ve gotta start somewhere..! So this post is to get you started with these constructs.<\/p>\n<h3><span style=\"font-family: Consolas;\">enum<\/span> in Java<\/h3>\n<p>&#8220;enum&#8221; is the short form for &#8220;enumeration&#8221;&#8230; Here, an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> will be an enumeration of constants. In <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>, what we do is &#8211;<\/p>\n<ul>\n<li>Declare an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>. Which involves declaring a set of constants.<\/li>\n<li>We create a variable of the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> type, which can hold any one value among the declared constants for that <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>&#8230; Nothing else&#8230;!<\/li>\n<\/ul>\n<p>Why would anyone use an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>..? &#8230; Well, it can look insignificant at most times, but there is a wide scope where we can use them&#8230;!<\/p>\n<p>Let&#8217;s take the men&#8217;s T-shirt sizes for example. We all know that the sizes have fixed letters associated with them. We don&#8217;t have to bother ourselves about all of them&#8230; Let&#8217;s just pick a few &#8211;<\/p>\n<ul>\n<li>S &#8211; Small<\/li>\n<li>M &#8211; Medium<\/li>\n<li>L &#8211; Large<\/li>\n<li>XL &#8211; Extra Large<\/li>\n<\/ul>\n<p>Now, if you wanted to create a variable to store the T-shirt sizes&#8230; Which data types would you choose..? And moreover, how will you ensure that the variable will have exactly anyone of these predefined values..? That&#8217;s where an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> comes into the picture.<\/p>\n<p>Let&#8217;s look at a simple code which uses an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Enumerations\n{\n\t\/\/ Declaring an enum\n\t\/\/ Give the set of constants\n\tenum TShirtSizes {S, M, L, XL};\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\t\/\/ Creating a variable of the enum\n\t\tTShirtSizes myShirt;\n\n\t\t\/\/ Assigning a value to the variable\n\t\t\/\/ Must assign a value among the\n\t\t\/\/ declared value\n\t\tmyShirt = TShirtSizes.L;\n\t\t\/\/ Access the declared value\n\t\t\/\/ using dot operator \n\n\t\tSystem.out.println(myShirt);\t\/\/ L\n\t}\n}\n<\/pre>\n<p>This is how we use an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>. As you can see, there are 3 steps involved in using an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> in Java &#8211;<\/p>\n<ol>\n<li><strong>Declaring<\/strong> &#8211; Declare an enumeration using the <b><span style=\"font-family: Consolas; color: blue;\">enum<\/span><\/b> keyword. Provide the set of constants in curly braces. One important thing is that, <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> must be declared outside a method. They can be declared even outside a class, but not locally (inside a method)..!\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {S, M, L, XL};\n<\/pre>\n<\/li>\n<li><strong>Creating a variable<\/strong> &#8211; We create the variable using the name of the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> declared. It acts like a data type, similar to a <b><span style=\"font-family: Consolas; color: blue;\">struct<\/span><\/b>.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nTShirtSizes myShirt;\n<\/pre>\n<\/li>\n<li><strong>Assigning<\/strong> &#8211; Assign a value to the variable created. We must always assign a value among the constants declared before. We access these values by using the dot operator on the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> name. Assignment of any other value gives a compilation error.\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nmyShirt = TShirtSizes.L;\n<\/pre>\n<\/li>\n<\/ol>\n<p>Those were about the legal declarations. Let&#8217;s look at some illegal declarations. The whole class is given as you could make a mistake at anyone of the steps &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass EnumGoneWrong\n{\n\tenum TShirtSizes = {S, M, L, XL};\n\t\/\/ No '='. This is not an array!\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tTShirtSizes myShirt;\n\n\t\tmyShirt = TShirtSizes.L;\n\n\t\tSystem.out.println(myShirt);\n\t}\n}\n\n\/\/ Another\nclass EnumGoneWrong\n{\n\tenum TShirtSizes {S, M, L, XL};\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tTShirtSizes myShirt;\n\n\t\tmyShirt = TShirtSizes.XXL;\n\t\t\/\/ XXL wasn't declared..!\n\n\t\tSystem.out.println(myShirt);\n\t}\n}\n\n\/\/ Another\nclass EnumGoneWrong\n{\n\tenum TShirtSizes {S, M, L, XL};\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tTShirtSizes myShirt;\n\n\t\tmyShirt = M;\n\t\t\/\/ It is TShirtSizes.M !\n\n\t\tSystem.out.println(myShirt);\n\t}\n}\n\n\/\/ Another\nclass EnumGoneWrong\n{\n\tenum TShirtSizes {S, M, L, XL};\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tTShirtSizes myShirt;\n\n\t\tmyShirt = 'M';\n\t\t\/\/ It is TShirtSizes.M !\n\n\t\tSystem.out.println(myShirt);\n\t}\n}\n\n\/\/ Another\nclass EnumGoneWrong\n{\n\tenum TShirtSizes {S, M, L, XL};\n\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tTShirtSizes myShirt;\n\n\t\tmyShirt = 1;\n\t\t\/\/ Cannot take any value\n\t\t\/\/ other than those declared !\n\n\t\tSystem.out.println(myShirt);\n\t}\n}\n<\/pre>\n<h2>Methods in Java<\/h2>\n<p>Functions in Java are actually called &#8220;methods&#8221;. That&#8217;s the terminology we are supposed to use here. In my previous posts, I have used the word &#8220;functions&#8221; more than &#8220;methods&#8221; so that you don&#8217;t feel too alien. But from now on, we use the term &#8220;methods&#8221;.<\/p>\n<p>Methods as you know it, are subroutines that can be called from a method, where the control passes to the called method, and comes back to the caller method after execution of the subroutine, with or without a return type.<\/p>\n<p>We will use a lot of methods when we start creating our own classes&#8230;. And by &#8220;a lot&#8221;, I mean really A LOT..! But for now, Let&#8217;s just learn how to call a method from the main() method.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Methods\n{\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tprintMessage(&quot;This was printed from a &quot;);\n\t}\n\n\tstatic void printMessage(String msg)\n\t{\n\t\tSystem.out.println(msg + &quot;method !&quot;);\n\t}\n}\n<\/pre>\n<p>Now the VERY IMPORTANT thing here is that, if you want to call any method from the main() method, it must be marked <b><span style=\"font-family: Consolas; color: blue;\">static<\/span><\/b>, otherwise it is a compilation error..! Not just main() method, any <b><span style=\"font-family: Consolas; color: blue;\">static<\/span><\/b> method can only call another <b><span style=\"font-family: Consolas; color: blue;\">static<\/span><\/b> method..! So the following code gives a compilation error &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nclass Methods\n{\n\tpublic static void main (String&#x5B;] args)\n\t{\n\t\tprintMessage(&quot;This was printed from a &quot;);\n\t}\n\n\tstatic void printMessage(String msg)\n\t{\n\t\tprintMessageToBob(msg + &quot;method &quot;);\n\t}\n\n\tvoid printMessageToBob(String msg)\n\t{\n\t\tSystem.out.println(msg + &quot;Bob !&quot;);\n\t}\n}\n<\/pre>\n<p>It is because we are calling a non-static method from a static method. This code gives compilation error which is pretty famous among the beginners &#8211;<\/p>\n<pre>Main.java:17: error: non-static method printMessageToBob(String) cannot be referenced from a static context\nprintMessageToBob(msg + \"method \");\n^\n1 error\n<\/pre>\n<p>It is famous among the beginners because they don&#8217;t know what static really means. In Java, static means a lot. We will have a detailed discussion later. But you must remember this one rule.<\/p>\n<h3>Components in a Method&#8217;s Declaration<\/h3>\n<p>Methods in Java have six components. They are &#8211;<\/p>\n<ol>\n<li><strong>Access Modifier<\/strong> &#8211; The access modifier define the scope of the method, that is, from where this method can\/cannot be accessed. Access modifiers is a separate topic that we will see later.<\/li>\n<li><strong>Return Type<\/strong> &#8211; This is the data type of the return value. It is <b><span style=\"font-family: Consolas; color: blue;\">void<\/span><\/b> if the method does not return anything.<\/li>\n<li><strong>Name<\/strong> &#8211; The name of the method.<\/li>\n<li><strong>List of Parameters<\/strong> &#8211; Then comes the list of parameters enclosed in parenthesis. The data type of the parameter should be mentioned first.<\/li>\n<li><strong>List of Exceptions<\/strong> &#8211; The method must declare unhandled exceptions using <b><span style=\"font-family: Consolas; color: blue;\">throws<\/span><\/b> keyword. We will discuss this later.<\/li>\n<li><strong>Body of the Method<\/strong> &#8211; The actual code of the method. Must be enclosed in curly braces.<\/li>\n<\/ol>\n<p>I would like to introduce another important term &#8211;<\/p>\n<p><strong>Method Signature<\/strong> &#8211; It is the name of\u00a0 the method + the list of parameters. It does not include the return type.<\/p>\n<p>Method Signature will be a very frequent term in the upcoming discussions. So, do remember this term well..! As I said before, we will confine ourselves only on calling methods from the main() method. So, you needn&#8217;t be bothered about Access Modifiers or List of Exceptions for now.<br \/>\nSome examples of methods, their signatures, and whether they can be called from the main() method or not &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/ Method\npublic static void main(String&#x5B;] args) {\n    \/\/ main() method\n}\n\n\/\/ Signature -\n\/\/ main(String&#x5B;] args)\n\n\/\/ Method\nvoid print() {\n    \/\/ Can't be called from main()\n}\n\n\/\/ Signature -\n\/\/ Print()\n\n\/\/ Method\nstatic String getMessage() {\n    \/\/ Can be called from main()\n    return &quot;Hello&quot;;\n}\n\n\/\/ Signature -\n\/\/ getMessage()\n\n\/\/ Method\nstatic void sort(int&#x5B;] arr) {\n    \/\/ Can be called from main()\n}\n\n\/\/ Signature -\n\/\/ sort(int&#x5B;] arr)\n\n\/\/ Method\nint distance(int x1, int y1, int x2, int y2) {\n    \/\/ can't be called from main()\n    return 0;\n}\n\n\/\/ Signature -\n\/\/ distance(int x1, int y1, int x2, int y2)\n\n\/\/ Method\npublic static void main(String args) {\n    \/\/ Can be called from main()\n    \/\/ But this is not &quot;the&quot; main()..!\n}\n\n\/\/ Signature -\n\/\/ main(String args)\n<\/pre>\n<p>The examples are self-explanatory. But do feel free to comment if you have any doubts..! \ud83d\ude42<\/p>\n<h3>Passing Variables of Primitive Data Types<\/h3>\n<p>The primitive data types are always passed by value in Java. There are no pointers in Java. So&#8230; You cannot exactly create a swap function in Java..! I know you are a genius, but still, I want to remind you that this function does not swap the elements &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class SwapSwapSwap {\n\n    public static void main(String&#x5B;] args) {\n        int x = 10;\n        int y = 20;\n        \n        swap(x, y);\n        \n        System.out.println(&quot;x = &quot; + x);     \/\/ 10\n        System.out.println(&quot;y = &quot; + y);     \/\/ 20\n    }\n    \n    static void swap(int x, int y) {\n        int temp = x;\n        x = y;\n        y = temp;\n    }\n    \n}\n<\/pre>\n<h3>Passing Objects<\/h3>\n<p>The objects are passed by their object references, so &#8220;in-effect&#8221;, they are passed by reference. But passing an object doesn&#8217;t exactly mean that we can change them in the method called. That depends on whether the object passed is mutable or not. For example, we know that <span style=\"font-family: Consolas;\">String<\/span> objects are immutable, whereas objects of <span style=\"font-family: Consolas;\">StringBuilder<\/span> are mutable. So look at the following example &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class PassingAnObject {\n\n    public static void main(String&#x5B;] args) {\n        String str = &quot;String Object&quot;;\n        StringBuilder strb \n            = new StringBuilder(&quot;String Builder Object&quot;);\n        \n        changeString(str);\n        changeStringBuilder(strb);\n        \n        System.out.println(str);\n        \/\/ Gives - String Object\n        System.out.println(strb);\n        \/\/ Gives - String Builder Object Changed..!\n    }\n    \n    static void changeString(String str) {\n        str = str.concat(&quot; Changed..!&quot;);\n    }\n    \n    static void changeStringBuilder(StringBuilder strb) {\n        strb.append(&quot; Changed..!&quot;);\n    }\n    \n}\n<\/pre>\n<p>Strings are real tricky in this aspect. When we pass a <span style=\"font-family: Consolas;\">String<\/span> Object, initially, it does point to the object of the main() method, but once, we change it, we get a whole new object. This is not the case with objects of <span style=\"font-family: Consolas;\">StringBuilder<\/span>&#8230;! Objects of <span style=\"font-family: Consolas;\">StringBuilder<\/span> are mutable, so when we pass the object reference, the actual object can be modified by the called method, because inside the method, it&#8217;s just another reference pointing to the same <span style=\"font-family: Consolas;\">StringBuilder<\/span> object created in the main() method.<br \/>\nYou may not understand this&#8230; That&#8217;s ok..! \ud83d\ude42 &#8230; But remember this that whether the object can me modified depends on whether the object is immutable or not.<\/p>\n<h3>Naming Conventions<\/h3>\n<ul>\n<li><strong><span style=\"font-family: Consolas;\">enum<\/span><\/strong>s &#8211; As discussed, <strong><span style=\"font-family: Consolas;\">enum<\/span><\/strong> is a set of constants. According to the Oracle Code Convention, constants must be named in all caps. Such as &#8211;\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {SMALL, MEDIUM, LARGE, EXTRALARGE};\n<\/pre>\n<\/li>\n<li><strong>Methods<\/strong> &#8211; Methods in Java follow the Camel Case Convention. Here, the first word is written in small letters and the rest of the words following are capitalized. Such as &#8211;\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nint binarySearch(int&#x5B;] arr, int key) {\n    \/\/ code\n}\n\nString toString(int&#x5B;] arr) {\n    \/\/ code\n}\n\nchar charAt(int index) {\n    \/\/ code\n}\n\nint lastIndexOf(String str) {\n    \/\/ code\n}\n<\/pre>\n<p>I don&#8217;t know each and every method in the Java Library, but all the methods I have seen so far strictly follow the naming convention. I think Java library is the best example for everything we study in Java.<\/li>\n<\/ul>\n<h3>More about an <span style=\"font-family: Consolas;\">enum<\/span><\/h3>\n<p>This is an advanced discussion&#8230; So beginners can feel free to skip this section&#8230; \ud83d\ude42 &#8230; There&#8217;s a lot more to an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> in Java. <strong><span style=\"font-family: Consolas;\">enum<\/span><\/strong>s are like really special (or strange \ud83d\ude1b ) classes. As I told before, <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>s can be declared outside a class &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {\n    S, M, L, XL;\n}\n\npublic class Enumerations {\n\n    public static void main(String&#x5B;] args) {\n        \/\/ code\n    }\n\n}\n<\/pre>\n<p>Being able to declare it outside a class doesn&#8217;t make it a special kind of class&#8230; Then what..? &#8230; We can have constructors, methods and variables inside an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>..!<br \/>\nTake our example of the <b><span style=\"font-family: Consolas;\">enum TShirtSizes<\/span><\/b>&#8230; Well, saying S, M, L, is good&#8230; But wouldn&#8217;t it be nice if we had data about their actual measurements too..? I mean&#8230; What if we could store that size L meant 40 &#8211; 42 inches..? Now that would be great, wouldn&#8217;t it..? So, now, we bring the variables and constructors of an enum into picture. Go through the following code closely &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {\n\n    \/\/ Constant(value associated)\n    S(36, 38), M(38, 40), L(40, 42), XL(42, 44);\n\n    \/\/ Values to be stored\n    final int START, END;\n    \n    \/\/ Constructor\n    private TShirtSizes(int START, int END) {\n        this.START = START;\n        this.END = END;\n    }\n\n}\n\npublic class Enumerations {\n\n    public static void main(String&#x5B;] args) {\n        TShirtSizes shirt = TShirtSizes.L;\n        System.out.println(shirt);  \/\/ L\n        System.out.println(shirt.START + &quot; - &quot; \n                                       + shirt.END);\n        \/\/ Prints -&gt; 40 - 42\n    }\n\n}\n<\/pre>\n<p>This is an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> with variables and a constructor. We cannot directly invoke the constructor. It is automatically called with the values specified at the time of declaring the constants. It is called when we try to create a variable of the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> type. Have a look at the following program &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {\n\n    \/\/ Constant(value associated)\n    S(36, 38), M(38, 40), L(40, 42), XL(42, 44);\n\n    \/\/ Values to be stored\n    final int START, END;\n    \n    \/\/ Constructor\n    private TShirtSizes(int START, int END) {\n        System.out.println(&quot;Enum's constructor&quot;);\n        this.START = START;\n        this.END = END;\n    }\n\n}\n\npublic class Enumerations {\n\n    public static void main(String&#x5B;] args) {\n        System.out.println(&quot;main() begins...&quot;);\n        TShirtSizes shirt = TShirtSizes.L;\n        System.out.println(shirt);  \/\/ L\n        System.out.println(shirt.START + &quot; - &quot; \n                                       + shirt.END);\n        \/\/ Prints -&gt; 40 - 42\n        \n        TShirtSizes shirt2 = TShirtSizes.M;\n        System.out.println(shirt2);  \/\/ M\n        System.out.println(shirt2.START + &quot; - &quot;\n                                        + shirt2.END);\n        \/\/ Prints -&gt; 38 - 40\n        System.out.println(&quot;End of main()&quot;);\n    }\n\n}\n<\/pre>\n<p>It gives the output &#8211;<\/p>\n<pre>main() begins...\nEnum's constructor\nEnum's constructor\nEnum's constructor\nEnum's constructor\nL\n40 - 42\nM\n38 - 40\nEnd of main()\n<\/pre>\n<p>So the constructor is called as many times as the number of constants in the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>, when we use it for the first time. If we don&#8217;t use it at all, then the constructor is never called.<br \/>\nWe can also have non-static methods in a <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>, which can use <b><span style=\"font-family: Consolas; color: blue;\">this<\/span><\/b> reference to refer to the calling object.<br \/>\n<b><span style=\"font-family: Consolas;\">enum<\/span><\/b> has a static method called values() which returns an array of the <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> constants. This method can be called from the main method &#8211;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic static void print() {\n    for (TShirtSizes shirt : TShirtSizes.values()) {\n        System.out.print(shirt);\n        System.out.print(&quot;(&quot; + shirt.START);\n        System.out.print(&quot; - &quot; + shirt.END);\n        System.out.println(&quot;)&quot;);\n    }\n}\n<\/pre>\n<p>So, we looked at <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> as a special class. Does this mean that we can extend an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> to other classes..? No..! We cannot extend <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> to other classes..!<\/p>\n<h3>Summary<\/h3>\n<ul>\n<li>Syntax for declaring an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> in Java &#8211;\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nenum TShirtSizes {S, M, L, XL};\n<\/pre>\n<\/li>\n<li>We can&#8217;t declare an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> locally, we must declare it outside the method.<\/li>\n<li>This is how we create a variable of <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> and assign a value to it &#8211;\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nTShirtSizes myShirt = TShirtSizes.L;\n<\/pre>\n<\/li>\n<li>Functions are actually called &#8220;methods&#8221; in Java.<\/li>\n<li>Only static methods can be called from another static method. The main() method being static, can only call other static methods.<\/li>\n<li>Methods in Java have six components &#8211;\n<ol>\n<li>Access Modifier<\/li>\n<li>Return Type<\/li>\n<li>Name<\/li>\n<li>List of Parameters<\/li>\n<li>List of Exceptions<\/li>\n<li>Body of the Method<\/li>\n<\/ol>\n<\/li>\n<li>Method signature is the name of the method + list of parameters. It does not include the return type.<\/li>\n<li>Primitive Data Types are always passed by value.<\/li>\n<li>Objects are passed through their object references, so, they produce a pass-by-reference effect.<\/li>\n<li>What really decides if an object can be modified inside a method is whether the object is mutable or not.<\/li>\n<li><b><span style=\"font-family: Consolas;\">enum<\/span><\/b>s are actually a special kind of classes in Java.<\/li>\n<li>We can have constructors, non-static and static variables and methods in a <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>.<\/li>\n<li><b><span style=\"font-family: Consolas;\">enum<\/span><\/b> has a static method which returns an array of <b><span style=\"font-family: Consolas;\">enum<\/span><\/b> type variables which have all the enum&#8217;s declared constants.<\/li>\n<li>We cannot extend an <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>.<\/li>\n<\/ul>\n<p>This was what you needed to know about <b><span style=\"font-family: Consolas;\">enum<\/span><\/b>s and methods in Java. I hope it helped you. If you have anything to ask about this topic, feel free to comment..! Keep practising..! Happy Coding..! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello people..! This is a new post in Java Tutorials &#8211; Enum and Methods in Java. In this post I will talk [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[46,62,65,67,77,90,91],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-java","tag-enums-in-java","tag-java","tag-java-programming-language","tag-java-tutorials","tag-methods-in-java","tag-passing-objects-in-java","tag-passing-variables-in-java"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=1425"}],"version-history":[{"count":0,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1425\/revisions"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}