{"id":16503,"date":"2016-10-21T08:07:18","date_gmt":"2016-10-21T08:07:18","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=16503"},"modified":"2020-08-31T12:49:04","modified_gmt":"2020-08-31T12:49:04","slug":"ocajp-static","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/","title":{"rendered":"OCAJP &#8211; Static Keyword in Java"},"content":{"rendered":"<p>This post is about the <a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/\">OCAJP<\/a> exam objective &#8220;<strong>Apply the static keyword to methods and fields.<\/strong>&#8220;<strong>You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables , What will be the output ?<\/strong>. Here we would explain about static variables, static methods and difference between static variables, instance variables.<\/p>\n<blockquote><p><a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-free-test\/\">25 Free Mock Exam Questions<\/a><\/p><\/blockquote>\n<ul>\n<li>We can apply static keyword to variables , methods , blocks,classes, interfaces.<\/li>\n<li>Applying static keyword to classes, interfaces is out of scope for OCAJP. It is in OCPJP.<\/li>\n<li>Static blocks isn&#8217;t mentioned in the exam objectives. It is good to have fair idea. The question may contain static block.<\/li>\n<li>Here, We would explain about static variables, static blocks and static methods.<\/li>\n<\/ul>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/#Static_variables\" >Static variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/#Static_Blocks\" >Static Blocks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/#Difference_between_instance_variables_and_static_variables\" >Difference between instance variables and static variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/#Static_methods\" >Static methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-static\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Static_variables\"><\/span>Static variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>If you write static keyword before a class level variable, it is static variable or class variable.<\/li>\n<li>static variables belong to a class.<\/li>\n<li>A static variable is shared by all of the objects of a class.<\/li>\n<li>If you done any operation in static variable with one object , that result will be reflected in every object.<\/li>\n<li>For example you assigned some value to static variable, that value will be updated in every object.<\/li>\n<li>You can access static variable with class name or reference variable of same class object.<\/li>\n<li>You can also directly use static variable in a method without specifying class name or reference variable of same class object before static variable.<\/li>\n<li>You can access static variable from static method and instance method.<\/li>\n<\/ul>\n<ul>\n<li>The below example shows how to access a static variable<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class Demo {\n\tstatic int a = 2;\n\tpublic static void main(String[] args) {\n\t\tDemo d = new Demo();\n\t\tSystem.out.println(Demo.a);\/\/prints 2\n\t\tSystem.out.println(d.a);\/\/prints 2\n\t\tSystem.out.println(a);\/\/prints 2\n\t}\n\n}\n\n<\/pre>\n<ul>\n<li>The below example shows how static variable changes when multiple objects changes its value.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class Demo {\n\tstatic int a = 2;\n\tpublic static void main(String[] a) {\n\t\tDemo d1 = new Demo();\n\t\tDemo d2 = new Demo();\n\t\tDemo d3 = new Demo();\n\t\td1.a = 5;\n\t\tSystem.out.println(d1.a + \" \" + d2.a + \" \" + d3.a); \/\/ prints 5 5 5\n\t\td3.a = 10;\n\t\tSystem.out.println(d1.a + \" \" + d2.a + \" \" + d3.a); \/\/ prints 10 10 10\n\t}\n\n}\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Static_Blocks\"><\/span>Static Blocks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>If static keyword is prefixed to block , it is static block.<\/li>\n<li>Static blocks are mainly used for initializing static variables.<\/li>\n<li>You can&#8217;t access instance variables from static block.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class Demo {\n\n\tstatic int a = 2;\n\tint b = 10;\n\n\tstatic {\n\t\ta = 8;\n\t\t\/\/ b=15;it gives compile time error if un commented\n\t}\n\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(a);\n\n\t}\n\n}\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Difference_between_instance_variables_and_static_variables\"><\/span>Difference between instance variables and static variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Instance variable is a variable which will have separate copy for each object.<\/li>\n<li>If you made any change with one reference variable that will not effect in another object.<\/li>\n<li>Static variable is a variable which will have only one copy which is shared amonge each object.<\/li>\n<li>If you made any change with one reference variable that will effect in another object.<\/li>\n<li>Let&#8217;s Look at the below example to understand this practically.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class Demo {\n\tstatic int a = 2;\n\tint b = 5;\n\n\tpublic static void main(String[] a) {\n\t\tDemo d1 = new Demo();\n\t\tDemo d2 = new Demo();\n\t\tDemo d3 = new Demo();\n\t\td1.a = 5;\n\t\td2.b = 7;\n\t\td3.b = 10;\n\t\tSystem.out.println(d1.a + \" \" + d2.a + \" \" + d3.a); \/\/ prints 5 5 5\n\t\tSystem.out.println(d1.b + \" \" + d2.b + \" \" + d3.b); \/\/ 5 7 10\n\n\t\td1.a = 5;\n\t\td2.b = 7;\n\t\td3.a = 10;\n\n\t\tSystem.out.println(d1.a + \" \" + d2.a + \" \" + d3.a); \/\/ prints 10 10 10\n\t}\n\n}\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Static_methods\"><\/span>Static methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Static methods aren\u2019t associated with objects and can\u2019t use any of the instance variables<br \/>\nof a class.<\/li>\n<li>You can define static methods to access or manipulate static variables<\/li>\n<li>You can call static method with class name or reference variable of same type from static method or instance method.<\/li>\n<li>You can also call static method without class name or reference variable of same type from static method or instance method.<\/li>\n<li>Static method can&#8217;t access instance methods and instance variables.Because these members will not available when static methods executes.<\/li>\n<\/ul>\n<pre>public class Demo {\n\n\tstatic int a = 2;\n\tint b = 5;\n\n\tpublic void instanceMethod() {\n\t\tSystem.out.println(\"instance method\");\n\n\t}\n\n\tpublic static void staticMethod() {\n\n\t\tSystem.out.println(a);\n\t\t\/\/ System.out.println(b); it gives compile time error if un commented.\n\t\t\/\/ instanceMethod(); it gives compile time error if un commented.\n\t}\n\n\tpublic static void main(String[] a) {\n\t\tDemo.staticMethod();\n\t\tDemo d = new Demo();\n\t\td.staticMethod();\n\t\tstaticMethod();\n\n\t}\n\n}\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables ,\u00a0you will be asked to answer the questions like what will be the out for the program.<\/p>\n<p><strong>If you are interested in practicing more questions, consider our <a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-practice-tests\/\">650+ mock exam questions for OCAJP certification exam<\/a>.<\/strong><\/p>\n<p><strong>If you are looking for any support or help on <a href=\"https:\/\/www.whizlabs.com\/blog\/how-to-prepare-for-ocajp-8-certification\/\" title=\"How to prepare for OCAJP 8 Certification\">preparing for OCAJP certification exam<\/a>, please contact our support or call us.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;Apply the static keyword to methods and fields.&#8220;You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables , What will be the output ?. Here we would explain about static variables, static methods and difference between static variables, instance variables. 25 Free Mock Exam Questions We can apply static keyword to variables , methods , blocks,classes, interfaces. Applying static keyword to classes, interfaces is out of scope for OCAJP. It is in OCPJP. Static blocks [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[1140],"class_list":["post-16503","post","type-post","status-publish","format-standard","hentry","category-java","tag-ocajp-8-preparation"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":2,"uagb_excerpt":"This post is about the OCAJP exam objective &#8220;Apply the static keyword to methods and fields.&#8220;You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables , What will be the output ?. Here we would explain about static&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=16503"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16503\/revisions"}],"predecessor-version":[{"id":71611,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16503\/revisions\/71611"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=16503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=16503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=16503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}