Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 30, 2014 Categories: Core Java Basics. No Comments on Scope and lifetime of variables in Java

In this article you will learn about the scope and lifetime of variables in a Java program. Different types of variables in Java have different scope and lifetime.

 

scope and lifetime of variables in Java

 

Scope of a variable refers to in which areas or sections of a program can the variable be accessed and lifetime of a variable refers to how long the variable stays alive in memory.

General convention for a variable’s scope is, it is accessible only within the block in which it is declared. A block begins with a left curly brace { and ends with a right curly brace }.

As we know there are three types of variables: 1) instance variables, 2) class variables and 3) local variables, we will look at the scope and lifetime of each of them now.

 

Instance Variables:

A variable which is declared inside a class and outside all the methods and blocks is an instance variable.

General scope of an instance variable is throughout the class except in static methods. Lifetime of an instance variable is until the object stays in memory.

Scope-of-instance-variable

 

Class Variables:

A variable which is declared inside a class, outside all the blocks and is marked static is known as a class variable.

General scope of a class variable is throughout the class and the lifetime of a class variable is until the end of the program or as long as the class is loaded in memory.

scope of class variables

 

 

Local Variables:

All other variables which are not instance and class variables are treated as local variables including the parameters in a method.

Scope of a local variable is within the block in which it is declared and the lifetime of a local variable is until the control leaves the block in which it is declared.

scope of local variables

 

Nested Scope:

In Java, we can create nested blocks – a block inside another block. In case of nested blocks what is the scope of local variables?

All the local variables in the outer block are accessible within the inner block but vice versa is not true i.e., local variables within the inner block are not accessible in the outer block. Consider the following example:

As you can see in the above program, line 14 generates an error as the variable y is not visible in the outer block and therefore cannot be accessed.

The summary of scope and lifetime of variables is as shown below:

scope and lifetime of variables summary

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me