{"id":313,"date":"2025-06-29T14:42:56","date_gmt":"2025-06-29T14:42:56","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=313"},"modified":"2025-07-16T10:14:45","modified_gmt":"2025-07-16T10:14:45","slug":"what-is-static-how-to-set-value-of-static-variable","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-static-how-to-set-value-of-static-variable\/","title":{"rendered":"What is Static, How to set value of static variable"},"content":{"rendered":"\n<p>In Java, the <code>static<\/code> keyword is used to indicate that a <strong>member belongs to the class itself<\/strong>, rather than to instances of the class. This means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>static variable<\/strong> is shared across all instances of the class.<\/li>\n\n\n\n<li>A <strong>static method<\/strong> can be called without creating an object.<\/li>\n\n\n\n<li>A <strong>static block<\/strong> runs once when the class is loaded.<\/li>\n\n\n\n<li>A <strong>static class<\/strong> (nested) can be used without an instance of the outer class.<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd39 What Is a Static Variable?<\/p>\n\n\n\n<p>A <strong>static variable<\/strong> is also known as a <strong>class variable<\/strong>. It is initialized only once, at the time of class loading, and <strong>shared among all objects<\/strong> of that class.<\/p>\n\n\n\n<p>\u2705 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Student {\n    static String college = \"ABC University\"; \/\/ static variable\n    String name;\n\n    Student(String name) {\n        this.name = name;\n    }\n\n    void display() {\n        System.out.println(name + \" studies at \" + college);\n    }\n\n    public static void main(String&#91;] args) {\n        Student s1 = new Student(\"Alice\");\n        Student s2 = new Student(\"Bob\");\n\n        s1.display(); \/\/ Alice studies at ABC University\n        s2.display(); \/\/ Bob studies at ABC University\n\n        \/\/ Changing static variable\n        Student.college = \"XYZ Institute\";\n\n        s1.display(); \/\/ Alice studies at XYZ Institute\n        s2.display(); \/\/ Bob studies at XYZ Institute\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udee0\ufe0f How to Set a Static Variable<\/p>\n\n\n\n<p>You can set a static variable in <strong>three ways<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>At declaration<\/strong>:<\/li>\n<\/ol>\n\n\n\n<p>static int count = 0;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. Inside a static block<\/strong> (for complex initialization):<\/p>\n\n\n\n<p>static {<br>count = 100;<br>}<\/p>\n\n\n\n<p>3. <strong>Using the class name<\/strong>:<\/p>\n\n\n\n<p>Student.college = &#8220;New College&#8221;;<\/p>\n\n\n\n<p>\ud83e\udde0 Key Points<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Static variables are <strong>memory-efficient<\/strong>.<\/li>\n\n\n\n<li>They are <strong>not tied to any object<\/strong>.<\/li>\n\n\n\n<li>You can access them using the <strong>class name<\/strong> directly.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn what the static keyword means in Java and how to set static variable values. Discover how it supports shared data across class instances efficiently<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}