In this post, you will learn to:
- Define scope of variables.
- Describe primitive variables.
- Describe reference variables.
Scope of Variables
There are two types of variables in Java. These are as follows:
- Primitive variable
- Reference variable
Primitive Variables
The first type is Primitive variable. A Primitive variable is used to store primitive data type values. Primitive variables can be of two types, depending on where they have been declared. They are declared as instance variables and local variables.
Instance Variables
An instance variable is a variable that is declared inside a class, but outside any method. Instance variables are the fields of a class and are initialized only when the class is instantiated. The resulting object created has its own copy of each field of the class depending on the type of keyword used with the variable.
These variables are active until the class is active. In other words, these variables can be accessed, until a reference to the class or object exists.
Local Variables
Variables declared inside a method are called local variables. These variables are created when the method is called. These variables are destroyed when the method is exited. Local variables can only be accessed inside the method. They cannot be accessed from any other method or anywhere else inside the class.
The following code snippet demonstrates the concept of primitive instance and local variables.
public class Vehicle { private int horsepower; // Instance Variable public void getEngineType() { int numberOfCylinders; // Local Variable horsepower = 1000; // Can be accessed } public void getVehicleType() { // Cannot access this variable from this method. numberOfCylinders = 8; } }
Reference Variables
Reference variables are used to store references to objects. They are declared to be of a certain type which can never be changed. However, they can be used to refer to any object of the declared type or a sub-type of the declared type. They can be declared as instance variables and local variables.
Instance Variables
A reference variable when declared as an instance variable is accessible throughout the class.These variables are initialized by default, when the class is instantiated. The scope of such an instance variable is the class which declares it and the life of such a variable is until there is a reference to the class.
Local Variables
Local variables cannot be marked abstract or static, but they can be marked final. Local variables do not get default values like instance variables. Hence, as a rule, they must be initialized before they can be used in the method. A local variable cannot be referenced from any code outside the method in which it is declared. It is possible to give the same name to a local variable as that of an instance variable. This technique is known as shadowing. This practice is generally discouraged, but if the application developer does want to give the same name to the local and instance variables for better readability of code then the use of the this keyword is recommended.
The following code snippet demonstrates the concept of reference instance and local variables.
public class PixelPoint { Pixel pix; // Instance Reference Variable public void showPixel() { Pixel newPixel; // Local Reference Variable } public void setPixel(Pixel pix) { // Initializing instance reference variable using this keyword. this.pix = pix; } }