Variables in Java are an integral part of the source code. These are permanent or temporary storage containers, depending upon the scope of the variables, that hold values for various purposes in Java program executions. Every variable in Java is associated with a particular data type. Data types are fixed throughout the scope of the program unless type casting is done. All variables start with a literal and can be followed by alphanumeric characters. It can essentially be treated as a memory location.
Table of Contents
Java segregates variables into three major types:
- Local Variables
- Instance Variables
- Static Variables
We have mainly two types of data types in Java:
- Primitive Data Type
- Non-primitive Type
Variables are ‘reserved are allocated in memory’. Therefore, variables essentially act as names of the particular memory locations. The word ‘variable’ can be broken down into “vary” and “able” which signifies that it’s value can be changed.
Example: int length = 100; //The variable here is of the name ‘data’ and has a data type of integer assigned to it. The value currently stored in length is 100.
Local Variables
Local Variables are variables that are initialized and used within the body of a particular method. The name ‘local’ is derived from the scope of such variables. The scope of any variable is the boundary inside which the variable can be edited, updated, and used for execution. In this case, the scope of such variables are only within that particular method where it is defined. Cross-method referencing of local variables is not allowed. For the other methods of your class, local variables that do not directly concern them are as good as non-existent. As a convention, local variables cannot be declared as ‘static’ type.
Instance Variables
Instance Variables are also referred to as global variables. Unlike local variables, the scope of instance variables is throughout the structure of a class. This is because instance variables are defined within the class framework before any method is written. Instance variables can be referred to by any method of your class. depending upon the need. It is not declared ‘static’. Since Java code is executed with instances, for every instance of the class in question, these variables do not change values, neither are instance variable values shared among instances.
Static Variables
Static Variables are always declared using the keyword ‘static’. Such variables have a higher scope that instance variables. These static variable values can be shared among instances as well since they do not change with every instance. In terms of memory allocation, static variables are created right after the class is loaded. It does not wait for objects to instantiate the class before getting assigned to memory.
Difference of Local, Instance, and Static Variables
Parameter | Local Variables | Instance Variables | Static Variables |
Scope | Restricted to a particular method. | Instance of a particular class. | Multiple instances of a particular class (global). |
Declaration | Does not require ‘static’ keyword | Does not require ‘static’ keyword | Requires ‘static’ keyword. |
Memory Allocation | After object creation | After object creation | After class is loaded. |
Example to understand the various types of variables in JAVA
class Test { int length = 100; //Instance Variable static int len = 100; //Static Variable void testM() { int k = 5; //Local Variable } }
In this example, we can have several methods each with a set of local variables. However all the methods will be able to access both the instance and static variables defined at the start of the class.
0 Comments