{"id":365,"date":"2015-12-14T07:20:35","date_gmt":"2015-12-14T07:20:35","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=365"},"modified":"2016-01-02T14:39:50","modified_gmt":"2016-01-02T14:39:50","slug":"types-of-variables","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/types-of-variables.html","title":{"rendered":"Types Of Variables In JAVA With Example"},"content":{"rendered":"<p>A variables can be defined as to reserve memory space to store the values. These can be initialized at the time of declaration or later on depending on the type of variable. There are basically three type of variable available in java: local variables, instance variables and static variables.<\/p>\n<hr \/>\n<h4><strong>Types Of Variables in JAVA:<\/strong><\/h4>\n<p>There are 3 types of variables in JAVA:<\/p>\n<p>1. Local Variable<br \/>\n2. Instance variable<br \/>\n3. Class Variable\/Static variable<\/p>\n<p><span style=\"color: #008000;\"><strong>Local variable:<\/strong><\/span><\/p>\n<ul>\n<li>These are also called as stack variable. Because they exist in stack memory<\/li>\n<li>It is mandatory to initialize the local variable. Otherwise you will get run time error from compiler<\/li>\n<li>These can be defined inside method, constructor or also inside block. The scope or life time of local variable destroyed with end of method completion.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>Instance variable:<\/strong><\/span><\/p>\n<ul>\n<li>Instance variable are also known as member variable or field<\/li>\n<li>These are associated with the object creation. As the object get created instance variable also get created<\/li>\n<li>These live on heap memory. In case, if you don\u2019t initialize instance variable with initial value these get default value at run time implicitly.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>Class Variable\/Static Variable:<\/strong><\/span><\/p>\n<ul>\n<li>These are loaded and initialized when class is loaded in JVM<\/li>\n<li>There exists only one copy of class variable<\/li>\n<li>They live on heap memory. If these variables are not initialized to some default value is assigned to them implicitly.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Examples Of Variables In JAVA:<\/strong><\/h4>\n<p>Given three type of variable are explained in the program written here. Simply to check the output from a java program you need JDK (java development kit) and editor to write the program (e.g. notepad, eclipse, netbeans etc).<\/p>\n<p>Here, in this concept the Eclipse editor is used to check the output for different type of variables.<\/p>\n<p>Open Eclipse editor, create new project and create class name TypeOfVariable and program file will be automatically save in TypeOfVariable.java. This is because when you write code in java. It is mandatory to save file name as name of class having main function defined with .java extension.<\/p>\n<pre>public class TypeOfVariable{\r\n\tpublic static int staticvariable;\r\n\tint instancevariable;\r\n\tpublic void printValue(){\r\n\t\tint localvariable;\r\n\t\tSystem.out.println(\"the value of staticvariable \\t\"+staticvariable);\r\n\t\tSystem.out.println(\"the value of instancevariable\\t\"+instancevariable);\r\n\t\tSystem.out.println(\"the value of localvariable \\t\"+localvariable);\r\n\t}\r\n\tpublic static void main(String args[]){\r\n\t\tTypeOfVariable object=new TypeOfVariable();\r\n\t\tobject.printValue();\r\n\t}\r\n}<\/pre>\n<p>Here, in this program three variables named as staticvariable, instancevariable and localvariable are declared as respective for static variable, instance variable and local variable.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> If you run this program without initializing localvariable you will get an compile time error. This is simple because local variable must be initialized otherwise it will show an error like &#8220;The local variable localvariable may not have been initialized&#8221;.<\/p>\n<p><strong>Correct Program Code With Initialization of Local Variable:<\/strong><\/p>\n<p>Simply just assign the initial value to localvariable in the same program. The change in output will reflect automatically the importance of this change. Because only local variable required to initialize with some value.<\/p>\n<p>Class\/Static and instance variable implicitly assign the null \/0\/ false value to these particular variable.<\/p>\n<pre>public class TypeOfVariable{\r\n\tpublic static int staticvariable;\r\n\tint instancevariable;\r\n\tpublic void printValue(){\r\n\t\tint localvariable = 10;\r\n\t\tSystem.out.println(\"the value of staticvariable \\t\"+staticvariable);\r\n\t\tSystem.out.println(\"the value of instancevariable\\t\"+instancevariable);\r\n\t\tSystem.out.println(\"the value of localvariable \\t\"+localvariable);\r\n\t}\r\n\tpublic static void main(String args[]){\r\n\t\tTypeOfVariable object=new TypeOfVariable();\r\n\t\tobject.printValue();\r\n\t}\r\n}<\/pre>\n<p>Our Output for above program is given here:<\/p>\n<pre>the value of staticvariable \u00a0\u00a0 \u00a00\r\nthe value of instancevariable\u00a0\u00a0 \u00a00\r\nthe value of localvariable \u00a0\u00a0 \u00a010<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A variables can be defined as to reserve memory space to store the values. These can be initialized at the time of declaration or later on depending on the type of variable. There are basically three type of variable available in java: local variables, instance variables and static variables. Types Of Variables in JAVA: There &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/types-of-variables.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Types Of Variables In JAVA With Example<\/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-365","post","type-post","status-publish","format-standard","hentry","category-archieve","category-variables"],"psp_head":"<title>Types Of Variables In JAVA With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on 3 types of variables local variables, instance variables and static variables in JAVA with example. A variables can be defined as to reserve memory space to store the values. These can be initialized at the time of declaration or later on depending on the type of variable.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/types-of-variables.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/365","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=365"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}