Variables in Java with Best Explanation and Examples
What is Variables in Java?
The Variables in Java is a data container that saves the data values while the Java program is running.
Every variable is given a data type that tells what kind and how many values it can hold.
A variable is a name for a place in memory where the data is kept.
The Variable in Java is a name for a place in memory. It is the basic way that a program stores information.
- During program execution, the value stored in a variable can be changed.
- A variable is just a name for a memory location. All of the operations done on the variable affect that memory location.
- All variables must be declared in Java before they can be used.
How to Declare Variables in Java?
As an example, the picture below shows how we can declare variables in Java.
From the image, it’s clear that when declaring a variable, we need to pay attention to two things:
- datatype — Type of data that can be saved or stored in this variable.
- data_name — The name given to the variable.
In this way, only a memory location can be given a name.
There are two ways to give it a value:
- Variable Initialization
- Giving value by taking input
How to Initialize Variables
Variables are explicitly initialized if they are given a value in the declaration statement.
During processing, if a value is given to a variable, this is called implicit initialization.
It can be understood with the help of three things, which are:
- datatype — Type of data that can be saved or stored in this variable.
- data_name — The name given to the variable.
- value — It is the value that the variable was set to at first.
Illustrations
float sampleInterest;
// Declaring float variable
int number = 30, speed = 50;
// Declaring and initializing integer variable
Get JOKEN VILLANUEVA’s stories in your inbox
Join Medium for free to get updates from this writer.
char var = ‘g’;
// Declaring and initializing character variable
Types of Variables in Java
Now let’s talk about the different types of variables, which are as follows:
- Local Variables
- Instance Variables
- Static Variables
Here is an image of what the 3 Variables look like.
Local Variables in Java
The Local variables in Java are made when you enter a method, constructor, or block.
When you leave a method, constructor, or block, the variable is destroyed.
Local variables can’t use access modifiers.
Local variables can only be seen inside the method, constructor, or block where they were declared.
Example of Local Variables Program:
/*package whatever //do not write package name here */
// Contributed by Glenn Azuelo
import java.io.*;
public class pies {
public static void main(String[] args)
{
int variable = 26; // Declared a Local Variable
// This variable is local to this main method only
System.out.println("Local Variable: " + variable);
}
}Output:
Local Variable: 26📌To continue the discussion click the link below: ⬇️⬇️⬇️
Conclusion
The Variables in Java are essential for storing and managing data during program execution.
By understanding how to declare, initialize, and use different types of variables — local, instance, and static — you can write more organized and efficient code.
With the right data type and structure, variables help bring logic and flexibility to every Java application.
If you find this article valuable, please consider leaving a comment below and sharing your thoughts.
Your feedback will not only help us improve our content but also benefit others in the community by providing diverse insights and experiences.
Itsourcecode.com🚀
Thank you for being a part of the Itsourcecode community!
Before you leave, please consider the following:
I would appreciate it if you could show your support by clapping 50 times and following the author.
Follow us on [Pinterest]
Follow us on [KO-FI]
Follow us on [Facebook]

![Top 50+ Final Year Projects & Ideas for Computer Science with Source Code[2026]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*0N5Ajiqsllf2zE9e-jtJPA.png)
![TOP 15 ASP. Net Projects and Ideas with Free Source Code [2026]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*GvHyTZs3eTAXHZEeuFw6Pw.png)

![Uncaught TypeError: cannot read property ‘addeventlistener’ of null [SOLVED]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*CJ9uadIqArLe6uCWBZ_tdQ.png)




