Types of Variables: Part 1
Explore the different types of variables in Java including reference, local, instance, and class variables. Understand their scope, lifecycle, and how they store data and state within methods and objects. This lesson will help you grasp the fundamentals necessary for managing variable behavior in Java applications.
Reference variables
Now, we can give a summary of a few key rules about variables. We have found three types of variables so far:
- Reference variable
- Local variable
- Instance variable
The reference variables are directly related to the object scope. We have seen some problems and studied their nature through those problems.
Local variables
Local variables have some special characteristics. They are declared within a block, or method, or constructor. When the method is called, or the block is entered, these local variables are created and destroyed when we leave the block or if the call is returned from the method. We cannot access these variables outside this block of code. Furthermore, we have to initialize local variables.
Therefore, we need to remember a few key factors about local variables and eventually, we can compare its position with the object state or field, of which we have discussed earlier.
Just like objects store their fields in instance variables, a method stores its temporary states in local variables. The syntax for declaration is the same. We don’t use any local keywords for that. According to its location, we can assume whether the role of the variable is local or not. It is always between the opening and closing brackets of a method. Moreover, it is visible only inside the method.
Instance variables
The specialty of instance variables is directly related to the classes and objects. We can declare it outside any block, method, or constructor; but it must be inside a class. It is not mandatory that we should initialize the instance variable. Its default value is 0. Objects store their states inside the fields or variables and that value is unique to each instance or object.
Furthermore, its life cycle is totally dependent on the objects of the class. The creation and destruction process is controlled by objects. Since they are not static variables, only objects can access them depending on the declaration of the access modifiers. We will talk about access modifiers in the coming chapters.
Class variables (static fields)
We call a variable a class variable when its field is declared with the static modifier. The word static means you can access the variable directly through the class name. You don’t have to create an object to do that anymore. It also tells the compiler that there is exactly one copy of this variable in existence. It does not matter how many times you instantiate a class or how many objects you create, there is only one copy of this variable and only the class can access it.
We can think of a field like human eyes, and it can be marked as static. Why? Conceptually, we can apply the same number of eyes to all instances of human beings. Additionally, we can add the keyword final to assert that the number of eyes will never change.
Coding example: 19
Let’s take a look at the coding example given below:
Code explanation
We have seen an example of a class variable with a static modifier.
We will see some more examples that will have class variables with the static modifier.
Coding example: 20
Using get and set methods, we can modify the state of any protected and private field. This coding example deals with it. Let’s take a look:
Coding example: 21
In this example, we will recapitulate the method level scope of a local variable. As we have said earlier, a local variable is only visible inside the method of a class. You should treat it as a typical property of a method because a method stores its temporary state in those local variables. Watch this example where we try to access a local variable outside the method level scope.
Code explanation
The method eat() stores its state inside the local variable weight whose data type is int. However, the reference variable child wants to access it directly and tries to change its state.
Once we have commented out line 19, the program runs perfectly well.