{"id":1239,"date":"2022-02-26T06:35:55","date_gmt":"2022-02-26T06:35:55","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1239"},"modified":"2022-02-26T06:35:55","modified_gmt":"2022-02-26T06:35:55","slug":"java-enum-strings","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-enum-strings\/","title":{"rendered":"Java enum Strings"},"content":{"rendered":"\n<p>In this tutorial, we will learn to learn about string values for enum constants. We will also learn to override default string value for enum constants with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enum-strings\">Java enum Strings<\/h2>\n\n\n\n<p>Before you learn about enum strings, make sure to know about\u00a0Java enum.<\/p>\n\n\n\n<p>In Java, we can get the string representation of enum constants using the&nbsp;<code>toString()<\/code>&nbsp;method or the&nbsp;<code>name()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n   SMALL, MEDIUM, LARGE, EXTRALARGE\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n\n      System.out.println(\"string value of SMALL is \" + Size.SMALL.toString());\n      System.out.println(\"string value of MEDIUM is \" + Size.MEDIUM.name());\n\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>string value of SMALL is SMALL\nstring value of MEDIUM is MEDIUM\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have seen the default string representation of an enum constant is the name of the same constant.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"change-default-string\">Change Default String Value of enums<\/h2>\n\n\n\n<p>We can change the default string representation of enum constants by overriding the&nbsp;<code>toString()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Size {\n   SMALL {\n\n      \/\/ overriding toString() for SMALL\n      public String toString() {\n        return \"The size is small.\";\n      }\n   },\n\n   MEDIUM {\n\n     \/\/ overriding toString() for MEDIUM\n      public String toString() {\n        return \"The size is medium.\";\n      }\n   };\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      System.out.println(Size.MEDIUM.toString());\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The size is medium.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, we have created an enum&nbsp;<var>Size<\/var>. And we have overridden the&nbsp;<code>toString()<\/code>&nbsp;method for enum constants&nbsp;<code>SMALL<\/code>&nbsp;and&nbsp;<code>MEDIUM<\/code>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;We cannot override the&nbsp;<code>name()<\/code>&nbsp;method. It is because the&nbsp;<code>name()<\/code>&nbsp;method is&nbsp;<code>final<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn to learn about string values for enum constants. We will also learn to override default string value for enum constants with the help of examples. Java enum Strings Before you learn about enum strings, make sure to know about\u00a0Java enum. In Java, we can get the string representation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[556],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1239"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1239"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1239\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}