Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes

Scope and Lifetime of Variables || Java Programming Basics || Bcis Notes

Scope and Lifetime of Variables

Scope andĀ  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.

There are three types of variables:

  1. Instance variables
  2. Argument variables
  3. Local variables

Instance variables

  • 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’s automatic garbage collector later on.

Argument variables

  • 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.

Local variables

  • 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.
  • 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.
  • 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.

You may also likeĀ Literals and Variables

Be the first to comment

Leave a Reply

Your email address will not be published.


*