The scope of a variable defines 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 till now.
There are there types of variables: instance variables, formal parameters or local variables and 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. The lifetime of these variables is the same as the lifetime of the object to which it belongs. Object once created do not exist for ever. They are destroyed by the garbage collector of Java when there are no more reference to that object. - Argument variables
These are the variables that are defined in the header of a 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 argument or local variables.
In general, a set of curly brackets { } defines a particular scope. In Java you can usually access a variable as long as it was defined within the same set of brackets as the code you are writing or within any curly brackets inside of the curly brackets where the variable was defined.