{"id":12919,"date":"2023-02-13T07:56:23","date_gmt":"2023-02-13T07:56:23","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=12919"},"modified":"2023-03-02T06:43:16","modified_gmt":"2023-03-02T06:43:16","slug":"difference-between-static-and-non-static-variables-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/","title":{"rendered":"Difference between Static and Non-Static Variables in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<p>Static and Non Static Variables are two different types of variables in Java.<br \/>\nStatic variables are shared by all objects of a class and have a single instance, while non-static variables are unique to each object and have different values for different objects.<\/p>\n<p>Let us first learn about the Static Variables and Non-Static Variables, before discussing the major differences between Static and Non Static in Java.<\/p>\n<h2>Static Variables in Java<\/h2>\n<p>Static variables in Java are class-level variables that are shared among all objects of a class. They are declared using the &quot;static&quot; keyword and are stored in the class area. Because they are shared, their values can be accessed and modified by any object of the class. They are also known as class variables.<\/p>\n<p>Static variables are typically used to represent class-level data, such as the number of instances of a class that have been created, the maximum value allowed for a particular property, or a default value for a property.<\/p>\n<p>The static variables are initialized only once when the class is loaded and their values persist throughout the life cycle of the class.<\/p>\n<h3>Syntax for declaring Static Variables in Java:<\/h3>\n<pre><code>accessSpecifier static dataType variableName;<\/code><\/pre>\n<p>Here&#8217;s an example of declaring a static integer variable named count:<\/p>\n<pre><code>public static int count;<\/code><\/pre>\n<p>In this example, public is the access specifier, int is the data type, and count is the name of the static variable. The static keyword is used to specify that this is a static variable.<\/p>\n<p>It is also possible to initialize a static variable when it is declared, like this:<\/p>\n<pre><code>public static int count = 0;<\/code><\/pre>\n<p>In this example, the static variable count is initialized to the value 0 when it is declared.<\/p>\n<h3>Examples of Static Variables in Java<\/h3>\n<p>Here are some examples for a deep understanding of the Static Variables in Java:<\/p>\n<p><strong>Example 1:<\/strong> Counter Program in Java using Static Variables<br \/>\nThe value of the static variable remains the same across all the objects. The below Java code explains it in a neater way.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12478 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12478 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12478 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12478 .wpsm_nav-tabs > li.active > a, #tab_container_12478 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12478 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12478 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12478 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12478 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12478 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12478 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12478 .wpsm_nav-tabs > li > a:hover , #tab_container_12478 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12478 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12478 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12478 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12478 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12478 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12478 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12478 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12478 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12478 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12478 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12478 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12478\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12478\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12478_1\" aria-controls=\"tabs_desc_12478_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12478\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12478_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Example {\r\n  static int count = 0;\r\n\r\n  public Example() {\r\n    count++;\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    Example obj1 = new Example();\r\n    Example obj2 = new Example();\r\n    Example obj3 = new Example();\r\n\r\n    System.out.println(\"Number of objects created: \" + Example.count);\r\n  }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12478 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12478 a\"),jQuery(\"#tab-content_12478\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Number of objects created: 3<\/code><\/pre>\n<p><strong>Explanation:<\/strong><br \/>\nIn this example, count is a static variable that keeps track of the number of Example objects that have been created. Each time a new Example object is created, the value of count is incremented. Since count is a static variable, its value is shared across all Example objects and can be accessed directly via the class name (Example.count), rather than via an instance of the class.<\/p>\n<p><strong>Example 2:<\/strong> Area of Circle Program in Java using Static Variables<br \/>\nThe value of the static variable remains the same across all the objects. It can be used to declare constant such as pi, M, etc. in the Java Code.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12479 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12479 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12479 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12479 .wpsm_nav-tabs > li.active > a, #tab_container_12479 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12479 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12479 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12479 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12479 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12479 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12479 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12479 .wpsm_nav-tabs > li > a:hover , #tab_container_12479 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12479 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12479 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12479 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12479 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12479 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12479 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12479 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12479 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12479 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12479 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12479 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12479\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12479\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12479_1\" aria-controls=\"tabs_desc_12479_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12479\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12479_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Circle {\r\n  static double pi = 3.14;\r\n\r\n  double radius;\r\n\r\n  public Circle(double r) {\r\n    radius = r;\r\n  }\r\n\r\n  public double area() {\r\n    return pi * radius * radius;\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    Circle c1 = new Circle(5);\r\n    Circle c2 = new Circle(7);\r\n\r\n    System.out.println(\"Area of circle 1: \" + c1.area());\r\n    System.out.println(\"Area of circle 2: \" + c2.area());\r\n  }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12479 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12479 a\"),jQuery(\"#tab-content_12479\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Area of circle 1: 78.5\nArea of circle 2: 153.86<\/code><\/pre>\n<p><strong>Explanation:<\/strong><br \/>\nIn this example, pi is a static variable that is used to store the value of pi, which is a constant. Since the value of pi is the same for all circles, it makes sense to declare it as a static variable so that it can be shared across all instances of the Circle class. The radius instance variable, on the other hand, is unique to each individual Circle object and is therefore declared as a non-static (or instance) variable.<\/p>\n<h2>Non-Static Variables in Java<\/h2>\n<p>Non-static variables in Java are also known as instance variables. They are defined within a class but not marked as static. Unlike static variables, non-static variables are associated with an instance of a class and are stored in heap memory. This means that each object of the class will have its own copy of the non-static variables.<\/p>\n<p>Non-static variables are used to represent object-level data and are typically used to store data that is unique to each instance of a class. They can be accessed and modified using object references.<\/p>\n<p>The non-static variables are created each time an object is created and are destroyed when the object is garbage collected. Their values can be changed at any time during the life cycle of the object.<\/p>\n<h3>Syntax for declaring Non-Static Variables in Java:<\/h3>\n<p>Non-static (also known as instance) variables in Java are declared using the following syntax:<\/p>\n<pre><code>accessSpecifier dataType variableName;<\/code><\/pre>\n<p>Here, access modifier can be any of public, private, protected, or omitted (default). Data type is the type of the variable, such as int, String, etc. The variable name is the name you choose for the variable.<\/p>\n<p>Here\u2019s an example to show the syntax and declaration of static variable in Java:<\/p>\n<pre><code>private int count;\npublic String name;\nfloat balance;<\/code><\/pre>\n<h3>Example of Non-Static Variables in Java<\/h3>\n<p>Non-static (also known as an instance) variables in Java are specific to a single instance of a class and can hold different values for each object of the class.<\/p>\n<p>Here is an example of how to declare and use non-static variables in Java:<br \/>\nExample: BankAccount Class with Non-Static Variables in Java<br \/>\nWe will see the initialization on Non-Static Variables as per the syntax discussed above in the following code:<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12481 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12481 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12481 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12481 .wpsm_nav-tabs > li.active > a, #tab_container_12481 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12481 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12481 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12481 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12481 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12481 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12481 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12481 .wpsm_nav-tabs > li > a:hover , #tab_container_12481 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12481 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12481 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12481 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12481 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12481 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12481 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12481 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12481 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12481 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12481 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12481 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12481\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12481\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12481_1\" aria-controls=\"tabs_desc_12481_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12481\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12481_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class BankAccount {\r\n    private int accountNumber;\r\n    private String accountHolderName;\r\n    private double balance;\r\n\r\n    public BankAccount(int accountNumber, String accountHolderName, double balance) {\r\n        this.accountNumber = accountNumber;\r\n        this.accountHolderName = accountHolderName;\r\n        this.balance = balance;\r\n    }\r\n\r\n    public int getAccountNumber() {\r\n        return accountNumber;\r\n    }\r\n\r\n    public String getAccountHolderName() {\r\n        return accountHolderName;\r\n    }\r\n\r\n    public double getBalance() {\r\n        return balance;\r\n    }\r\n}\r\n\r\nclass Main{\r\n    public static void main(String args[]){\r\n        BankAccount account1 = new BankAccount(12345, \"John Doe\", 5000.0);\r\n        BankAccount account2 = new BankAccount(67890, \"Jane Doe\", 6000.0);\r\n\r\n        System.out.println(\"Account Number: \" + account1.getAccountNumber());\r\n        System.out.println(\"Account Holder Name: \" + account1.getAccountHolderName());\r\n        System.out.println(\"Balance: $\" + account1.getBalance());\r\n\r\n        System.out.println(\"Account Number: \" + account2.getAccountNumber());\r\n        System.out.println(\"Account Holder Name: \" + account2.getAccountHolderName());\r\n        System.out.println(\"Balance: $\" + account2.getBalance());\r\n\r\n    }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12481 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12481 a\"),jQuery(\"#tab-content_12481\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Account Number: 12345\nAccount Holder Name: John Doe\nBalance: $5000.0\nAccount Number: 67890\nAccount Holder Name: Jane Doe\nBalance: $6000.0<\/code><\/pre>\n<p><strong>Explanation:<\/strong><br \/>\nIn this example, the BankAccount class has three non-static variables: accountNumber, accountHolderName, and balance. Each instance of the class will have its own unique values for these variables. The class has a constructor method that takes these values as arguments and assigns them to the corresponding variables using the this keyword. The class also has getter methods to retrieve the values of these variables. In the main method, we create objects of the BankAccount class and assign values to their instance variables.<br \/>\nEach object of the BankAccount class has its own unique values for the accountNumber, accountHolderName, and balance variables because of the non-static nature of variables.<\/p>\n<p>Let us now discuss the difference between Static and Non Static in Java in detail.<\/p>\n<h2>Difference between Static and Non Static Variables in Java<\/h2>\n<p>In Java Programming Language, the Static and Non Static variables differ a lot. The table given below summarizes the key differences between the static and non static variables in Java:<\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>Static Variable<\/th>\n<th>Non-Static Variable<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Definition<\/td>\n<td>A static variable is a variable that is declared using the static keyword.<\/td>\n<td>A non-static variable is a variable that is not declared using the static keyword.<\/td>\n<\/tr>\n<tr>\n<td>Memory Allocation<\/td>\n<td>Allocated in class area and shared among all objects.<\/td>\n<td>Allocated in heap memory and created for each object.<\/td>\n<\/tr>\n<tr>\n<td>Access<\/td>\n<td>Can be accessed using class name.<\/td>\n<td>Can be accessed using object reference.<\/td>\n<\/tr>\n<tr>\n<td>Scope<\/td>\n<td>Scope is limited to the class.<\/td>\n<td>Scope is limited to the object.<\/td>\n<\/tr>\n<tr>\n<td>Initialization<\/td>\n<td>Initialized only once when the class is loaded.<\/td>\n<td>Initialized each time an object is created.<\/td>\n<\/tr>\n<tr>\n<td>Usage<\/td>\n<td>Used to represent class-level data.<\/td>\n<td>Used to represent object-level data.<\/td>\n<\/tr>\n<tr>\n<td>Example<\/td>\n<td>public static int count = 0;<\/td>\n<td>public int age;<\/td>\n<\/tr>\n<tr>\n<td>Inheritance<\/td>\n<td>Can be inherited by the subclass.<\/td>\n<td>Cannot be inherited by the subclass.<\/td>\n<\/tr>\n<tr>\n<td>Modification<\/td>\n<td>Can be modified using class name.<\/td>\n<td>Can be modified using object reference.<\/td>\n<\/tr>\n<tr>\n<td>Usage in Method<\/td>\n<td>Can be used in both static and non static in java.<\/td>\n<td>Can be used only in non-static methods.<\/td>\n<\/tr>\n<tr>\n<td>Life Cycle<\/td>\n<td>It is created when the class is loaded and destroyed when the class is unloaded.<\/td>\n<td>It is created when the object is created and destroyed when the object is garbage collected.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Conclusion<\/strong><br \/>\nWe have covered the static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature. The static keyword can be used to manage memory. In contrast to non-static methods, which cannot be used without constructing an object, static methods may be made available without generating an object. With the help of explanations and examples, we have discussed static and non static variables and methods in Java.<\/p>\n<p>Let us go through some of the Frequently Asked Question on Static and Non Static in Java.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p><strong>Q: When to use static variable in Java?<\/strong><br \/>\n<strong>A:<\/strong> Use static variable in Java when you want to share the same value across all instances of a class, for example, a constant or a counter shared by all instances.<\/p>\n<p><strong>Q: When to use non-static variable in Java?<\/strong><br \/>\n<strong>A:<\/strong> Use non-static variable in Java when you want to store instance-specific data, for example, instance-specific attributes or variables with different values for different instances.<\/p>\n<p><strong>Q: How to access static and non-static variables in Java?<\/strong><br \/>\n<strong>A:<\/strong> You can access static variables using the class name followed by the dot operator (.) and the variable name. Non-static variables can be accessed using an instance of the class followed by the dot operator (.) and the variable name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Static and Non Static Variables are two different types of variables in Java. Static variables are shared by all objects of a class and have a single instance, while non-static variables are unique to each object and have different values for different objects. Let us first learn about the Static Variables and Non-Static Variables, before [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-12919","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Difference between Static and Non-Static Variables in Java<\/title>\n<meta name=\"description\" content=\"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference between Static and Non-Static Variables in Java\" \/>\n<meta property=\"og:description\" content=\"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-13T07:56:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-02T06:43:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Difference between Static and Non-Static Variables in Java\",\"datePublished\":\"2023-02-13T07:56:23+00:00\",\"dateModified\":\"2023-03-02T06:43:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\"},\"wordCount\":1388,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\",\"name\":\"Difference between Static and Non-Static Variables in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\",\"datePublished\":\"2023-02-13T07:56:23+00:00\",\"dateModified\":\"2023-03-02T06:43:16+00:00\",\"description\":\"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Difference between Static and Non-Static Variables in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference between Static and Non-Static Variables in Java","description":"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Difference between Static and Non-Static Variables in Java","og_description":"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.","og_url":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-13T07:56:23+00:00","article_modified_time":"2023-03-02T06:43:16+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Difference between Static and Non-Static Variables in Java","datePublished":"2023-02-13T07:56:23+00:00","dateModified":"2023-03-02T06:43:16+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/"},"wordCount":1388,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/","url":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/","name":"Difference between Static and Non-Static Variables in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg","datePublished":"2023-02-13T07:56:23+00:00","dateModified":"2023-03-02T06:43:16+00:00","description":"Know about static and non static variables in Java in detail. Variables and methods in Java are non-static by default, but the main() function is static in nature.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676274926144-Difference%20between%20Static%20and%20Non-Static%20Variables%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/difference-between-static-and-non-static-variables-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Difference between Static and Non-Static Variables in Java"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=12919"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12919\/revisions"}],"predecessor-version":[{"id":12993,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12919\/revisions\/12993"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=12919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=12919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=12919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}