{"id":438,"date":"2019-08-19T12:21:18","date_gmt":"2019-08-19T06:36:18","guid":{"rendered":"https:\/\/bcisnotes.com\/secondsemester\/?p=438"},"modified":"2020-05-05T09:01:22","modified_gmt":"2020-05-05T03:16:22","slug":"scope-and-lifetime-of-variables","status":"publish","type":"post","link":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/","title":{"rendered":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes"},"content":{"rendered":"<h2>Scope and Lifetime of Variables<\/h2>\n<p>Scope and\u00a0 Lifetime of variables define the section of the code in which the variable is visible. As a general rule, variables that are defined within a block are not accessible outside that block. The lifetime of a variable refers to how long the variable exists before it is destroyed. Destroying variables refers to deallocating the memory that was allotted to the variables when declaring it. We have written a few classes until now. You might have observed that not all variables are the same. The ones declared in the body of a method were different from those that were declared in the class itself.<\/p>\n<p>There are three types of variables:<\/p>\n<ol>\n<li>Instance variables<\/li>\n<li>Argument variables<\/li>\n<li>Local variables<\/li>\n<\/ol>\n<p><strong>Instance variables<\/strong><\/p>\n<ul>\n<li>Instance variables are those that are defined within a class itself and not in any method or constructor of the class. They are known as instance variables because every instance of the class (object) contains a copy of these variables. The scope of instance variables is determined by the access specifier that is applied to these variables. We have already seen it earlier. The lifetime of these variables is the same as the lifetime of the object to which it belongs. An object once created does not exist forever. They are destroyed by the garbage collector of Java when there is no more reference to that object. We shall see about Java&#8217;s automatic garbage collector later on.<\/li>\n<\/ul>\n<p><strong>Argument variables<\/strong><\/p>\n<ul>\n<li>These are the variables that are defined in the header oaf constructor or a method. The scope of these variables is the method or constructor in which they are defined. The lifetime is limited to the time for which the method keeps executing. Once the method finishes execution, these variables are destroyed.<\/li>\n<\/ul>\n<p><strong>Local variables<\/strong><\/p>\n<ul>\n<li>A local variable is the one that is declared within a method or a constructor (not in the header). The scope and lifetime are limited to the method itself.<\/li>\n<li>One important distinction between these three types of variables is that access specifiers can be applied to instance variables only and not to an argument or local variables.<\/li>\n<li>In addition to the local variables defined in a method, we also have variables that are defined in bocks life an if block and an else block. The scope and is the same as that of the block itself.<\/li>\n<\/ul>\n<p>You may also like\u00a0<a href=\"https:\/\/bcisnotes.com\/secondsemester\/literals-and-variables\/\">Literals and Variables<\/a><\/p>\n<div class=\"tlplf69faba2878faf\" ><div id=\"amzn-assoc-ad-668fe681-bdc6-49ee-a9f9-a4c2f5be29a0\"><\/div><script async src=\"\/\/z-na.amazon-adsystem.com\/widgets\/onejs?MarketPlace=US&adInstanceId=668fe681-bdc6-49ee-a9f9-a4c2f5be29a0\"><\/script><\/div><style type=\"text\/css\">\r\n@media screen and (min-width: 1201px) {\r\n.tlplf69faba2878faf {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 993px) and (max-width: 1200px) {\r\n.tlplf69faba2878faf {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 769px) and (max-width: 992px) {\r\n.tlplf69faba2878faf {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (min-width: 768px) and (max-width: 768px) {\r\n.tlplf69faba2878faf {\r\ndisplay: block;\r\n}\r\n}\r\n@media screen and (max-width: 767px) {\r\n.tlplf69faba2878faf {\r\ndisplay: block;\r\n}\r\n}\r\n<\/style>\r\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Scope and Lifetime of Variables Scope and\u00a0 Lifetime of variables define the section of the code in which the variable is visible. As a general <a class=\"mh-excerpt-more\" href=\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\" title=\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":6,"featured_media":856,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes<\/title>\n<meta name=\"description\" content=\"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\" \/>\n<meta property=\"og:description\" content=\"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"BCIS NOTES\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-19T06:36:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-05T03:16:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"340\" \/>\n\t<meta property=\"og:image:height\" content=\"230\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Octopus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Octopus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\"},\"author\":{\"name\":\"Octopus\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/1e7cf3abcf4849e9d66fc9f6b67ee861\"},\"headline\":\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\",\"datePublished\":\"2019-08-19T06:36:18+00:00\",\"dateModified\":\"2020-05-05T03:16:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\"},\"wordCount\":411,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#organization\"},\"image\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg\",\"articleSection\":[\"Object Oriented Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\",\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\",\"name\":\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\",\"isPartOf\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg\",\"datePublished\":\"2019-08-19T06:36:18+00:00\",\"dateModified\":\"2020-05-05T03:16:22+00:00\",\"description\":\"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.\",\"breadcrumb\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage\",\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg\",\"contentUrl\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg\",\"width\":340,\"height\":230,\"caption\":\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/bcisnotes.com\/secondsemester\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#website\",\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/\",\"name\":\"BCIS NOTES\",\"description\":\"Second Semester\",\"publisher\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/bcisnotes.com\/secondsemester\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#organization\",\"name\":\"BCIS NOTES\",\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/12\/cropped-2.jpg\",\"contentUrl\":\"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/12\/cropped-2.jpg\",\"width\":300,\"height\":100,\"caption\":\"BCIS NOTES\"},\"image\":{\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/1e7cf3abcf4849e9d66fc9f6b67ee861\",\"name\":\"Octopus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e31a69c6a6d830dca58523a537833871?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e31a69c6a6d830dca58523a537833871?s=96&d=mm&r=g\",\"caption\":\"Octopus\"},\"url\":\"https:\/\/bcisnotes.com\/secondsemester\/author\/sujan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes","description":"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.","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:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/","og_locale":"en_US","og_type":"article","og_title":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes","og_description":"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.","og_url":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/","og_site_name":"BCIS NOTES","article_published_time":"2019-08-19T06:36:18+00:00","article_modified_time":"2020-05-05T03:16:22+00:00","og_image":[{"width":340,"height":230,"url":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg","type":"image\/jpeg"}],"author":"Octopus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Octopus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#article","isPartOf":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/"},"author":{"name":"Octopus","@id":"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/1e7cf3abcf4849e9d66fc9f6b67ee861"},"headline":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes","datePublished":"2019-08-19T06:36:18+00:00","dateModified":"2020-05-05T03:16:22+00:00","mainEntityOfPage":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/#organization"},"image":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg","articleSection":["Object Oriented Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/","url":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/","name":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes","isPartOf":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/#website"},"primaryImageOfPage":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage"},"image":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg","datePublished":"2019-08-19T06:36:18+00:00","dateModified":"2020-05-05T03:16:22+00:00","description":"Scope and Lifetime of Variables defines the section of the code in which the variable is visible,variables that are defined within a block.","breadcrumb":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#primaryimage","url":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg","contentUrl":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/08\/scopeandlifetime.jpg","width":340,"height":230,"caption":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes"},{"@type":"BreadcrumbList","@id":"https:\/\/bcisnotes.com\/secondsemester\/object-oriented-programming\/scope-and-lifetime-of-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/bcisnotes.com\/secondsemester\/"},{"@type":"ListItem","position":2,"name":"Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes"}]},{"@type":"WebSite","@id":"https:\/\/bcisnotes.com\/secondsemester\/#website","url":"https:\/\/bcisnotes.com\/secondsemester\/","name":"BCIS NOTES","description":"Second Semester","publisher":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/bcisnotes.com\/secondsemester\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/bcisnotes.com\/secondsemester\/#organization","name":"BCIS NOTES","url":"https:\/\/bcisnotes.com\/secondsemester\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/logo\/image\/","url":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/12\/cropped-2.jpg","contentUrl":"https:\/\/bcisnotes.com\/secondsemester\/wp-content\/uploads\/2019\/12\/cropped-2.jpg","width":300,"height":100,"caption":"BCIS NOTES"},"image":{"@id":"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/1e7cf3abcf4849e9d66fc9f6b67ee861","name":"Octopus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/bcisnotes.com\/secondsemester\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e31a69c6a6d830dca58523a537833871?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e31a69c6a6d830dca58523a537833871?s=96&d=mm&r=g","caption":"Octopus"},"url":"https:\/\/bcisnotes.com\/secondsemester\/author\/sujan\/"}]}},"_links":{"self":[{"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/posts\/438"}],"collection":[{"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/comments?post=438"}],"version-history":[{"count":3,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/posts\/438\/revisions"}],"predecessor-version":[{"id":1003,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/posts\/438\/revisions\/1003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/media\/856"}],"wp:attachment":[{"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/media?parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/categories?post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bcisnotes.com\/secondsemester\/wp-json\/wp\/v2\/tags?post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}