Variables in Java programming are identifiers that help identify the memory location of a data member’s value. Variables help store and manipulate data, making them a fundamental software program requirement. Programmers must declare variables before specifying the data type they will hold. It is an essential condition that programmers must fulfill when using a variable in a program.
How to Declare the Variable in Java?
The basic format of a variable declaration includes prefixing the datatype to the variable and assigning a value. Programmers can assign a value in two ways: either while declaring the variable itself or while writing the program. Below is the sample for variable declaration,
Here, int is the data type
empid is the variable name
and 27 is the value assigned to the variable
The format of the syntax includes,
datatype variable = value
Types of Variables in Java:
There are three types of variables in Java. They include local variables, instance variables, and static/class variables.
As an extra note, we will see the structure of Java programming to understand the types of variables in Java.
- While writing a Java program, we first write the class and then the method.
- A typical example of a method includes the main method.
- Immediately after defining the class, there is variable declaration.
Local Variables:
- Programmers declare local variables inside methods or blocks.
- Since local variables do not contain a default value, assigning the initial value should be done while declaring the variables and even before using them.
- Internally, the implementation of local variables is at the stack level.
- Moreover, a program creates local variables when a method is invoked and destroys them when the method exits.
Instance Variables:
- Instance variables are declared inside a class.
However, they are outside the method, or block. - Programmers use constructors while assigning values to instance
variables. - The heap stores all instance variables that are part of an object.
- We use the “new” keyword to create an instance variable.
- The JVM dynamically creates objects using the new keyword along with a constructor. Importantly, JVM also handles garbage collection automatically, deallocating memory when objects are no longer in use. Lastly, the creation and deallocation of instance variables occur along with the object.
Static Variables:
- Static variables are class variables.
- If we use the static keyword for a data member, it becomes a static variable.
- Static variables execute first during program execution.
- The initialization of static variables takes place only once.
- Static members remain accessible throughout the program or until the program’s completion.
- Static members or class members have only one instance for all the instances of the class.
- All instances or objects access the static members inside the stack area.
Some interesting details about variables:
- While the identifiers that point toward the primitive datatypes are variables, the identifiers that point toward the objects are known as reference variables.
- Cannot use Keywords for variables. Because, Java contains keywords (reserved words) such as boolean, byte, and others. They carry particular meaning for the code. Hence, programmers should be cautious while giving names to variables.
- The maximum length of a variable is 256 characters.
- The variable starts with an underscore (_) or dollar symbol ($). However, we can rarely see the use of $.
- There should not be any space between the variables—for instance, in “empid,” there is no space between emp and id.
- In a variable, the first letter should be an alphabet, while we can use a number or a character from the second letter.
- However, we cannot use special characters for variables except for $ and _ at the beginning.
- Variables are case-sensitive.
- Variables help define an object’s state in Java programming language and hence play a significant role during object creation. (In Java, importantly, an object contains state and behavior).
For expert guidance, click on the link.