In this article, we will discuss Variables in Java Programming, Declaration of variables, and Types of variables (Local, Instance, and Static) in detail with java Programming examples.
What is a Variable in Java?
Variables are the blocks or containers that store the data. Every variable is of specific data types which signify the kind of data the variable can hold. In easier words, we can say that a variable is a name given to a block of memory that holds data or, we can say that a variable is a name given to a memory location.
The value stored in a variable can be changed anytime within a program
Memory Blocks and Variables in java
Let’s take a deep understanding of memory blocks and variables which will help us in better learning Programming –
- The whole memory of the system is divided into blocks of data. When we create a variable, some part of the memory block is reserved by the variable and a specific name is assigned to the memory block which is the name of the variable.
- The size of the memory block that is reserved in the name of the variable depends upon the data type of the variable.
- The data type of a variable refers to the kind of data that the variable will store.
- A variable of type integer can store an integer value, whereas a variable of type character can store some character.
- So, the size of memory varies from data type to data type for example integer data type is 4 bytes which means if a variable of integer type is declared, 4 bytes of memory is reserved.
- The size of this variable also depends upon the architecture of the system which means it is different for a 32-bit architecture machine and different for a 64-bit architecture machine.
Declaration of variables in Java
The image below shows an example of how a variable is declared in Java.
From the image is it understood that the first part defines the data type of the variable followed by the name of the variable. This name can be anything but there are some rules that need to be followed while defining the name. The rules are given below –
- It cannot be from any of the pre-defined keywords – Which means that the name cannot be from any of the keywords that are pre-defined in java for any other specific purpose. Examples of the pre-defined keywords are class, throws, etc.
- It cannot contain spaces – Which means the name of the variable should be of one word only. int Yuvayana is a valid keyword whereas int Yuvayana tech is not a valid keyword and this will lead to an error in the program.
- The first keyword cannot be a digit – Which means that the variable name cannot start with a digit. 15yuvayana will not be considered as a valid variable name whereas yuvayana15 is valid.
After the declaration of variables, the second necessary step is initialization.
This means we have reserved a block of memory but, now we will keep our data in that. So, we will not learn about the initialization of variables –
Types of Variables
There are three types of variables. Local variables, Instance Variables, Static Variables
Local Variables–
A variable defined within a block of function is called its local variable. The scope of this variable is restricted to the block, which means the variables can be accessed only within the block. Initialization of the variables is mandatory if we are using it within a specific block.
E.g. –
class yuvayana {
Public static void main (String args []) {
int var = 100;
}
}
In this example, the var is declared as a variable inside the main class. Thus, it cannot be accessed by any other class or from outside the main class. This type of variable is called a local variable whose scope is limited to a specific class.
Instance variables–
These are non-static variables that are defined within a specific class. These can be used only after creating an object of the block. Initialization of the instance variables is not necessary as the default value of these variables is set 0.
E.g. –
class yuvayana {
int n ; //declaration
Public static void main(String args[]){
yuvayana y = new yuvayana ();
y.n = 100; //accessing the variable
}
}
In this example, the variable n is declared inside the class yuvayana. The variable is accessed by using the class name and a dot(.) operator.
Static Variables–
These variables are defined in a similar way as an instance variables.
The difference is that they are declared using the static keywords. Static variables are created when the execution of the program starts and destroyed automatically after the execution ends. We can also access the static variables like an instance variable but it will lead to a warning which will be shown in the console but the program will still run.
class apple {
public static String name = “Yuvayana Tech”;
public static void main (String[] args) {
system.out.prinln(“The name is :” + apple.name);
}
So, this article was about the datatypes and variables which are the basic thing that needs to be learned while learning programming in a certain language.
Now it’s time to attempt – Data Types and Variable in Java Quiz