{"id":637,"date":"2016-01-19T14:03:35","date_gmt":"2016-01-19T14:03:35","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=637"},"modified":"2016-01-19T14:03:35","modified_gmt":"2016-01-19T14:03:35","slug":"local-variable-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/local-variable-example.html","title":{"rendered":"Local Variable With Example In JAVA"},"content":{"rendered":"<p>A local variable is a variable declared inside a method body, block or constructor. It means variable is only accessible inside the method, block or constructor that declared it.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>In java, a block i.e. &#8220;area between opening and closing curly brace&#8221; defines a scope. Each time you start a new block, you begin a new scope.<\/p>\n<p><span style=\"color: #008000;\"><strong>Scope of Local Variable:<\/strong><\/span><\/p>\n<p>Scope of local variable are limited to method. Such a variable is accessible only within the method or block in which it is declared. The local variable&#8217;s scope starts from the line they are declared and their scope remains there until the closing curly brace of the method comes.<\/p>\n<p><span style=\"color: #008000;\"><strong>Declaration of Local Variable:<\/strong><\/span><\/p>\n<p>Every local variable declaration statement is contained by a block ({ &#8230; }). We can also declare the local variables in the header of a \u201cfor\u201d statement. In this case it is executed in the same manner as if it were part of a local variable declaration statement.<\/p>\n<p><strong>For example<\/strong>: <code>for(int i=0;i&lt;=5;i++){\u2026\u2026}<\/code><\/p>\n<p>In above example int i=0 is a local variable declaration. Its scope is only limited to the for loop.<\/p>\n<hr \/>\n<h4><strong>Syntax Of Local Variable:<\/strong><\/h4>\n<pre>methodname(){\r\n&lt;DataType&gt; localvarName;\r\n}<\/pre>\n<p>Here methodname is the name of method, DataType refers to data type of variable like int, float etc and localvarName is the name of local variable.<\/p>\n<p>Example of Sytax of Local variable<\/p>\n<pre>int area()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int length=10; \/\/local variable\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int breadth = 5; \/\/local variable\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int rectarea = length*breadth; \/\/local variable\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return rectarea;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}<\/pre>\n<hr \/>\n<h4><strong>Program Example of Local Variable With Explanation:<\/strong><\/h4>\n<p>Let us take an example in which we take local variable to explain it. Here,\u00a0<strong>age<\/strong>\u00a0is a local variable. This variable is defined under <strong>putAge()<\/strong>\u00a0method and its scope is limited to this method only:<\/p>\n<pre>public class Dog\r\n\r\n{\r\n\r\n\u00a0\u00a0 \u00a0public void putAge()\r\n\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0int age = 0; \/\/local variable\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0age = age + 6;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Dog age is : \" + age);\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String args[]){\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Dog d = new Dog();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0d.putAge();\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Dog age is : 6<\/pre>\n<hr \/>\n<h4><strong>Important Points To Remember:<\/strong><\/h4>\n<ul>\n<li>Local variables are declared in a blocks, methods or constructors.<\/li>\n<li>Local variables are created when the block, method or constructor is started and the variable will be destroyed once it exits the block, method or constructor.<\/li>\n<li>Access modifiers cannot be used for declaring local variables.<\/li>\n<li>Local variables are implemented at stack level internally.<\/li>\n<li>Default values are not assigned to a local variables in Java.<\/li>\n<li>Variables can also be define inside statement blocks i.e. do, for, while loop. These are only visible inside that block.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A local variable is a variable declared inside a method body, block or constructor. It means variable is only accessible inside the method, block or constructor that declared it. Important Note: In java, a block i.e. &#8220;area between opening and closing curly brace&#8221; defines a scope. Each time you start a new block, you begin &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/local-variable-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Local 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":[74],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-archieve","category-variables","tag-local-variable-scope"],"psp_head":"<title>Local Variable With Example In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Understand Local Variable and its scope in JAVA with example. A local variable is a variable declared inside a method body, block or constructor. It means variable is only accessible inside the method, block or constructor that declared it.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/local-variable-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/637","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=637"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}