{"id":545,"date":"2010-07-05T13:54:42","date_gmt":"2010-07-05T08:24:42","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=545"},"modified":"2010-07-05T13:54:42","modified_gmt":"2010-07-05T08:24:42","slug":"static-block-static-variable-and-static-method","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/static-block-static-variable-and-static-method\/","title":{"rendered":"Static Block, Static Variable and Static Method"},"content":{"rendered":"<p>In java the static keyword leads to following important \u00a0\u00a0concepts \u00a0, namely<\/p>\n<p>1) Static Variable<\/p>\n<p>2) Static Block<\/p>\n<p>3) Static Method<\/p>\n<p><strong>Static \u00a0\u00a0Variables<\/strong><\/p>\n<p>Static variables are the special type of variable which are created during the load time of class, or we can say when the class is instantiated. The syntax for declaring variable as static is as follows,<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-546\" title=\"static Block Static Method Static Variable\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable.png?resize=502%2C102&#038;ssl=1\" alt=\"static Block Static Method Static Variable\" width=\"502\" height=\"102\" \/><\/a><\/p>\n<p>Some important points regarding static variables are<\/p>\n<ol>\n<li>It should be accessed by <strong>class name.<\/strong><\/li>\n<li>Only one instance is created and it is shared by all the data members.<\/li>\n<\/ol>\n<p>This can be further explained as follows \u00a0. Consider an example consisting of static and Non static member<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable1.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-547\" title=\"static Block Static Method Static Variable\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable1.png?resize=496%2C239&#038;ssl=1\" alt=\"static Block Static Method Static Variable\" width=\"496\" height=\"239\" \/><\/a><\/p>\n<p>In above example when class is instantiated multiple copies of instance variable k is created\u00a0 where each copy would going to point a particular object at a \u00a0time, but in case of static only one copy is created and shared among all objects &amp; methods. As shown in fig<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-548\" title=\"static Block Static Method Static Variable\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable2.png?resize=331%2C153&#038;ssl=1\" alt=\"static Block Static Method Static Variable\" width=\"331\" height=\"153\" \/><\/a><\/p>\n<div id=\"_mcePaste\"><strong>Static Block:<\/strong><\/div>\n<div id=\"_mcePaste\">Static block&#8217;s are the block which are implicitly called when the class is instantiated that means when the instance of class is made. (by \u00a0using new operator)<\/div>\n<p>Static Block:Static block&#8217;s are the block which are implicitly called when the class is instantiated that means when the instance of class is made. (by \u00a0using new operator)<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable3.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-549\" title=\"static Block Static Method Static Variable\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable3.png?resize=433%2C164&#038;ssl=1\" alt=\"static Block Static Method Static Variable\" width=\"433\" height=\"164\" \/><\/a><\/p>\n<p class=\"MsoNormal\" style=\"text-indent: .5in;\">When the above program is executed , for each instance of class StaticBlockDemo static block is called , but keep in mind it is called only when the class is instantiated, not declared.<\/p>\n<p class=\"MsoNormal\"><strong style=\"mso-bidi-font-weight: normal;\">Static Methods:<\/strong><\/p>\n<p class=\"MsoNormal\"><strong style=\"mso-bidi-font-weight: normal;\">Some points about Static method<span style=\"mso-spacerun: yes;\"> <\/span>are<\/strong><\/p>\n<ol>\n<li><span style=\"mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;\"><span style=\"mso-list: Ignore;\"><span style=\"font: 7.0pt &quot;Times New Roman&quot;;\"> <\/span><\/span><\/span>Static methods are automatically created during load time of class.<\/li>\n<li><span style=\"mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;\"><span style=\"mso-list: Ignore;\"><span style=\"font: 7.0pt &quot;Times New Roman&quot;;\"> <\/span><\/span><\/span>They are accessed by using class name.<\/li>\n<li><span style=\"mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;\"><span style=\"mso-list: Ignore;\"><span style=\"font: 7.0pt &quot;Times New Roman&quot;;\"> <\/span><\/span><\/span>A single copy of static method is created and is shared among all other methods.<\/li>\n<li><span style=\"mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;\"><span style=\"mso-list: Ignore;\"><span style=\"font: 7.0pt &quot;Times New Roman&quot;;\"> <\/span><\/span><\/span>The most important point <strong style=\"mso-bidi-font-weight: normal;\">&#8220;Static function cannot access non static methods\/memeber&#8221;\u009d<\/strong>.<\/li>\n<\/ol>\n<p class=\"MsoListParagraphCxSpLast\">Declaration of static method is given as follows<\/p>\n<p class=\"MsoListParagraphCxSpLast\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable4.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-550\" title=\"static Block Static Method Static Variable\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/07\/static-Block-Static-Method-Static-Variable4.png?resize=406%2C163&#038;ssl=1\" alt=\"static Block Static Method Static Variable\" width=\"406\" height=\"163\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Infrormation about Static Block, Static Variable and Static Method in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"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,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[191],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-java","tag-static"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":545,"position":0},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. 152 . Which SOQL statement can be used to get all records even from recycle\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1612,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/serialization-marshalling-deflating-in-java\/","url_meta":{"origin":545,"position":1},"title":"Serialization \/ Marshalling \/ Deflating in JAVA","author":"Jitendra","date":"March 9, 2011","format":false,"excerpt":"Concept of Serialization \/ Marshalling \/ Deflating in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Serialization ,Marshalling ,deflating, DeSerialization ,UnMarshalling ,inflating","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Serialization-Marshalling-deflating-DeSerialization-UnMarshalling-inflating.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1629,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/nested-class-and-its-necessity-with-example\/","url_meta":{"origin":545,"position":2},"title":"Nested Class and its necessity with example","author":"Jitendra","date":"March 11, 2011","format":false,"excerpt":"Example and Introduction of Nested classes in JAVA and there Types","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Nested Class in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Nested-Class-in-JAVA.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1937,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/virtual-function-in-java\/","url_meta":{"origin":545,"position":3},"title":"Virtual Function in JAVA","author":"Jitendra","date":"April 8, 2011","format":false,"excerpt":"Example and explanation of Virtual Function in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5152,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-test-loaddata-to-import-records-with-relationship\/","url_meta":{"origin":545,"position":4},"title":"Using Test.loadData to import records with relationship","author":"Jitendra","date":"January 6, 2016","format":false,"excerpt":"There are many resources and documents available around how to use Test.loadData to create test records in Apex class. As per best practice of writing Test classes in Apex, Its good idea to store master data (aka Seed, Reference data) in static resource and load\u00a0it in Test classes using \"Test.loadData\"\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Test.loadData and Static resource in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2681,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-5\/","url_meta":{"origin":545,"position":5},"title":"Salesforce Interview Questions &#8211; Part 5","author":"Jitendra","date":"January 24, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=545"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/545\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}