{"id":642,"date":"2016-01-19T14:11:12","date_gmt":"2016-01-19T14:11:12","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=642"},"modified":"2016-01-19T14:11:12","modified_gmt":"2016-01-19T14:11:12","slug":"static-variable-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/static-variable-example.html","title":{"rendered":"Static Variable With Example In JAVA"},"content":{"rendered":"<p>Any variable when declared with the keyword \u201cstatic\u201d is known as static variable or class variable in JAVA. Static variable is used to fulfill the common properties of all objects. For example: institute name of students is common for all students so it will be declared as static variable in JAVA. <strong>The static variable gets memory at class loading time only once in class area.<\/strong><\/p>\n<p><strong><span style=\"color: #008000;\">Syntax of static variable:<\/span> <\/strong><\/p>\n<p><code>static &lt;DataType&gt; &lt;variable_name&gt;;<\/code><\/p>\n<p><strong>For example:<\/strong><\/p>\n<p>Below age is a static variable.<\/p>\n<pre>static int age;<\/pre>\n<hr \/>\n<h4><strong>Initialization of Static Variable:<\/strong><\/h4>\n<ol>\n<li>Static\u00a0variables\u00a0are initialized when class is loaded.<\/li>\n<li>Static variables in a class are initialized before any object of that\u00a0class can be created.<\/li>\n<li>Static variables are initialized before any static method\u00a0of the class runs.<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>When To Create Static Variable:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Problem Without Using Static Variable<\/strong><\/span><\/p>\n<p>First let&#8217;s take a program example without using static variable and see what problem occur. Suppose a college name GGGI wants to store students details name and roll number.<\/p>\n<pre>class Student\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int rollno;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0String name;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0String college=\"GGGI\";\r\n\u00a0\u00a0 \u00a0}<\/pre>\n<p><strong>Problem:<\/strong> Suppose there are 600 students in GGGI institute. When object is created, all instance data members will get memory each time. All students have its unique rollno and name so instance data member is good. But here, institute refers to the common property of all objects. So if we make it static, this field will get memory only once in a class area at class loading time.<\/p>\n<p><span style=\"color: #008000;\"><strong>Solution Using Static Variable<\/strong><strong>:<\/strong><\/span><\/p>\n<p>The below program has a class <strong>Student<\/strong> in which we use <strong>static<\/strong> variable college. This variable is common to all students so we make it static. The student information will be displayed with different roll no and names but same college. The advantage to make college variable static is that it save memory as it loads once in a class area at class loading time:<\/p>\n<pre>class Student\r\n\r\n{\r\n\u00a0\u00a0 \u00a0int rollno;\r\n\u00a0\u00a0 \u00a0String name;\r\n\u00a0\u00a0 \u00a0static String college =\"GGGI\"; \/\/Static Variable gets memory once\r\n\r\n\u00a0\u00a0 \u00a0\/*Constructor of Student class*\/\r\n\u00a0\u00a0 \u00a0Student(int r,String n)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0rollno = r;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0name = n;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/*Method For Displaying Student Details*\/\r\n\u00a0\u00a0 \u00a0void display()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(rollno+\" \"+name+\" \"+college); \/\/ print the value of roll no, name and college\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String args[])\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Student s1 = new Student(101,\"Gagan\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Student s2 = new Student(102,\"Raman\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0s1.display();\u00a0 \/\/ call the display function using the s1 object\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0s2.display();\u00a0\u00a0 \/\/ call the display function using the s2 object\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>101 Gagan GGGI\r\n102 Raman GGGI<\/pre>\n<hr \/>\n<h4><strong>\u00a0Importance Of Static Variable:<\/strong><\/h4>\n<p>With the help of static variable we make our program memory efficient. It means static variable saves memory.<\/p>\n<hr \/>\n<h4><strong>Important Points About Static Variable To Remember:<\/strong><\/h4>\n<ul>\n<li>Static variables are also known as Class variables which are declared with the \u201cstatic\u201d keyword in a class.<\/li>\n<li>A\u00a0single copy of each variable per class is to be shared by all instances of the class.<\/li>\n<li>Static variables are stored in static memory. Static variables are rarely used other than it is declared final and used as either public or private constants.<\/li>\n<li>When the program starts static variables are created and when the program stops, static variables are destroyed.<\/li>\n<li>Default values of static variables are same as instance variables. For numbers the default value is 0, for Boolean the default value is false and for object references the default value is null.<\/li>\n<li>When static or class variables are declared as public static final, then variables names are all in upper case. The naming syntax is the same as instance and local variables in case of static variables are not declared public and final.<\/li>\n<li>Static variables can be accessed by calling with the class name. It does not need any object.<\/li>\n<li>Static variables are\u00a0initialized only one time, i.e. at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Any variable when declared with the keyword \u201cstatic\u201d is known as static variable or class variable in JAVA. Static variable is used to fulfill the common properties of all objects. For example: institute name of students is common for all students so it will be declared as static variable in JAVA. The static variable gets &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/static-variable-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Static Variable With Example In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,65],"tags":[],"class_list":["post-642","post","type-post","status-publish","format-standard","hentry","category-archieve","category-variables"],"psp_head":"<title>Static Variable With Example In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Understand the concept of static variable, importance and when to declare them in JAVA with example and code. A static variable gets memory at class loading time only once in class area.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/static-variable-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":1462,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/642\/revisions\/1462"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}