{"id":180,"date":"2025-06-22T13:05:08","date_gmt":"2025-06-22T13:05:08","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=180"},"modified":"2025-07-16T10:31:06","modified_gmt":"2025-07-16T10:31:06","slug":"what-is-static-in-java","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-static-in-java\/","title":{"rendered":"What is static in java?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Let\u2019s break down <strong><code>static<\/code> in Java<\/strong> in the <strong>simplest possible way<\/strong>, step by step, and include examples to help you fully understand.<\/h4>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 What Does <code>static<\/code> Mean in Java?<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Think of <strong><code>static<\/code><\/strong> as meaning <strong>&#8220;belongs to the class, not to objects&#8221;<\/strong>.<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">In Java, you can write code that either:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Belongs to <strong>each individual object<\/strong> (like someone\u2019s name or age), or<\/li>\n\n\n\n<li>Belongs to the <strong>class itself<\/strong>, shared by <strong>all objects<\/strong> (like a common rule or counter). That\u2019s where <code>static<\/code> comes in.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf Real-Life Analogy<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Imagine a <strong>Car<\/strong> class:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each <strong>car object<\/strong> has its own <strong>color<\/strong> and <strong>speed<\/strong> (different for each car).<\/li>\n\n\n\n<li>But all cars <strong>share the same rule<\/strong>: they drive on the <strong>same road<\/strong> or <strong>follow the same speed limit<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">We can say:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>color<\/code> and <code>speed<\/code> are <strong>non-static<\/strong> (belong to each car).<\/li>\n\n\n\n<li><code>roadType<\/code> or <code>speedLimit<\/code> is <strong>static<\/strong> (shared by all cars, belongs to the class).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">In Java Terms<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 1. Static Variables (a.k.a Class Variables)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>public class Car {\n    \/\/ static variable - shared by all Car objects\n    static int numberOfCars = 0;\n\n    \/\/ instance variable - each car has its own\n    String color;\n\n    public Car(String color) {\n        this.color = color;\n        numberOfCars++; \/\/ Increase count when a new Car is made\n    }\n\n    public void display() {\n        System.out.println(\"Color: \" + color);\n        System.out.println(\"Total Cars: \" + numberOfCars);\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><code>public class Main {\n    public static void main(String&#91;] args) {\n        Car c1 = new Car(\"Red\");\n        Car c2 = new Car(\"Blue\");\n\n        c1.display();\n        c2.display();\n        \/\/ Output:\n        \/\/ Color: Red, Total Cars: 2\n        \/\/ Color: Blue, Total Cars: 2\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f <code>numberOfCars<\/code> is static \u2014 it&#8217;s <strong>shared<\/strong> and <strong>keeps track of all cars<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 2. Static Methods<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Static methods are methods that <strong>belong to the class<\/strong>, not objects. You don\u2019t need to create an object to use them.<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class MathUtil {<br>    public static int add(int a, int b) {<br>        return a + b;<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Main {<br>    public static void main(String[] args) {<br>        int result = MathUtil.add(5, 3);<br>        System.out.println(\"Result: \" + result); \/\/ Output: 8<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714\ufe0f You didn\u2019t need to write <code>new MathUtil()<\/code> \u2014 because <code>add()<\/code> is <strong>static<\/strong>.<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 3. Static Blocks<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Static blocks are used to <strong>initialize static data<\/strong>. It runs <strong>once<\/strong> when the class is loaded.<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Demo {<br>    static int x;<br><br>    static {<br>        x = 10;<br>        System.out.println(\"Static block executed\");<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc49 4. Static Classes (Nested)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">You can make a <strong>static inner class<\/strong> (a class inside another class that doesn&#8217;t need an outer object).<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Outer {<br>    static class Inner {<br>        public void show() {<br>            System.out.println(\"Inside static inner class\");<br>        }<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Main {<br>    public static void main(String[] args) {<br>        Outer.Inner obj = new Outer.Inner();<br>        obj.show();<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Summary Table<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">Feature<\/th><th class=\"has-text-align-center\" data-align=\"center\"><code>static<\/code><\/th><th class=\"has-text-align-center\" data-align=\"center\">Non-<code>static<\/code><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Belongs to<\/td><td class=\"has-text-align-center\" data-align=\"center\">Class<\/td><td class=\"has-text-align-center\" data-align=\"center\">Object<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Accessed using<\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>ClassName.member<\/code><\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>object.member<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Memory usage<\/td><td class=\"has-text-align-center\" data-align=\"center\">One copy shared by all<\/td><td class=\"has-text-align-center\" data-align=\"center\">Each object has its own<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Example use<\/td><td class=\"has-text-align-center\" data-align=\"center\">Utility methods, counters<\/td><td class=\"has-text-align-center\" data-align=\"center\">Personal data like name<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn what the static keyword means in Java. Discover how it defines class-level variables, methods, and blocks for efficient memory and shared access.<\/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-180","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\/180","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=180"}],"version-history":[{"count":5,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions\/461"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}