Java Variables
- In Java,a variable could be a container that holds information during the execution of a program.
- It acts as a named memory area that stores a value, which can be adjusted, recovered, or controlled depending on the logic of the program.

Variable
- Variables are fundamental building blocks of any Java program as they allow developers to work with data dynamically.
- Variables are nothing but peace of memory, which is capable of holding data or value.
- In Java, one variable can store only one value. If you try to assign one more value, then previous value will be override with the new value.
- In Java variables are also known as a data members because they are going to program.
Characteristics of Java Variables
- Data Type: Each variable in Java has a particular type that characterizes what kind of information it can store (e.g., integer, float, string).
- Memory Allotment: Java distributes memory for variables during runtime or compile time based on their type and scope.
- Scope: The accessibility of a variable depends on where it is declared (local, instance, or static).
- Initialization: Variables can either be initialized when declared or later before being used. Some variables are given default values automatically.
Components of a Variable
A Java variable has the following components:
- Name: A unique identifier (also known as the variable name).
- Data Type: Specifies the type of data (e.g.,
int
,float
,String
). - Value: The actual data stored in the variable.
- Scope: Defines where the variable is accessible.
- Lifetime: Determines how long the variable exists in memory.
Syntax for Declaring a Variable
Example:
Types of Variables in Java
Java variables are categorized into three types based on how and where they are used:

1. Local Variables
- Definition: Declared inside a method, constructor, or block.
- Scope: Only accessible within the block or method where they are defined.
- Lifetime: Created when the method or block starts and destroyed once it exits.
- Key Points:
- Must be explicitly initialized before use.
- Used for temporary data storage in a method.
Example:
public class Example {
2. Instance Variables
- Definition: Declared inside a class but outside any method, constructor, or block.
- Scope: Belongs to the object of the class; each object gets its copy.
- Lifetime: Exists as long as the object exists.
- Key Points:
- Automatically initialized with default values (e.g.,
0
for int,null
for objects). - Represents the state or attributes of an object.
- Automatically initialized with default values (e.g.,
Example:
public class Example {
int number; // Instance variable
void display() {
System.out.println(“Instance variable: ” + number);
}
}
3. Static Variables
- Definition: Declared using the
static
keyword and belongs to the class rather than any specific object. - Scope: Shared among all instances of the class.
- Lifetime: Exists for the entire duration of the program’s runtime.
- Key Points:
- Only one copy of the variable exists, no matter how many objects are created.
- Can be accessed without creating an object of the class.
Example:
public class Example {
static int count = 0; // Static variable
void increment() {
count++;
}
}
Variable Scope
The scope of a variable defines the region in which it can be accessed.
- Class Scope: For instance and static variables, accessible anywhere in the class.
- Method Scope: For local variables, limited to the method where declared.
- Block Scope: For variables declared inside loops or condition blocks, limited to the block.
Variable Initialization
Variables can be initialized at the time of declaration or later. Some types of variables get default values, while others require explicit initialization.
Default Values for Variables
Data Type | Default Value |
---|---|
int | 0 |
float | 0.0 |
boolean | false |
char | \u0000 |
String | null |
Example Program
Here’s a program that demonstrates different types of variables:
Output:
Local Variable: 10
Instance Variable: 20
Static Variable: 50