{"id":3893,"date":"2017-08-07T02:48:56","date_gmt":"2017-08-07T02:48:56","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3893"},"modified":"2024-09-04T07:20:05","modified_gmt":"2024-09-04T07:20:05","slug":"java-static-block","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-static-block\/","title":{"rendered":"Java Static Block"},"content":{"rendered":"<h2>Java Static Block<\/h2>\n<p>A static block in Java is executed when the class loads and before the main method is executed. Let&#8217;s see the functioning of the static block in the below java program and understand the importance of the static block in Java. We can use this block as an initialization block in the automation code.<\/p>\n<h3>Java Program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.staticpkg;\r\n\r\n\/\/StaticBloclDemo.java\r\n\/\/www.TestingDocs.com\r\npublic class StaticBlockDemo {\r\n  static int count =0;\r\n  static {\r\n    \/\/This will execute before the main.\r\n    System.out.println(\"The static block is executed.\");\t \t \r\n  }\r\n\r\n  public static void main(String[] args) {\t \t \r\n    System.out.println(\"In Main method.\");\t \t \r\n  }\t \t \r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3903\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java.png\" alt=\"Static block Demo Java\" width=\"1724\" height=\"1034\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java.png 1724w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java-300x180.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java-1024x614.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java-768x461.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Static-block-Demo-Java-1536x921.png 1536w\" sizes=\"auto, (max-width: 1724px) 100vw, 1724px\" \/><\/h3>\n<h3><\/h3>\n<h3>Program Output<\/h3>\n<p>The static block is executed.<br \/>\nIn Main method.<\/p>\n<p>&nbsp;<\/p>\n<p>Java class can have multiple static blocks.All the blocks would be executed first.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.staticpkg;\r\n\r\n\/\/StaticBloclDemo.java\r\n\/\/www.TestingDocs.com\r\npublic class StaticBlockDemo {\r\n  static int count =0;\r\n  static {\r\n    \/\/static block 1.\r\n    System.out.println(\"The static block 1.\");\t \t \r\n  }\r\n\r\n  public static void main(String[] args) {\t \t \r\n    System.out.println(\"In Main method.\");\t \t \r\n  }\r\n  \r\n  static {\r\n    \/\/static block 2.\r\n    System.out.println(\"The static block 2.\");\t \t \r\n  }\r\n\r\n}\r\n<\/pre>\n<p>The static block 1.<br \/>\nThe static block 2.<br \/>\nIn Main method.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Static Block A static block in Java is executed when the class loads and before the main method is executed. Let&#8217;s see the functioning of the static block in the below java program and understand the importance of the static block in Java. We can use this block as an initialization block in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-3893","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3893"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3893\/revisions"}],"predecessor-version":[{"id":24066,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3893\/revisions\/24066"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}